What's the best way to make a time from “Today” or “Yesterday” and a time in Python?

前端 未结 2 837
挽巷
挽巷 2021-02-06 03:39

Python has pretty good date parsing but is the only way to recognize a datetime such as \"Today 3:20 PM\" or \"Yesterday 11:06 AM\" by creating a new date today and doing subtra

2条回答
  •  醉话见心
    2021-02-06 04:11

    I am not yet completely up to speed on Python yet, but your question interested me, so I dug around a bit.

    Date subtraction using timedelta is by far the most common solution I found.

    Since your question asks if that's the only way to do it, I checked out the strftime format codes to see if you could define your own. Unfortunately not. From Python's strftime documentation:

    ... The full set of format codes supported varies across platforms, because Python calls the platform C library’s strftime() function, and platform variations are common.

    The following is a list of all the format codes that the C standard (1989 version) requires ...

    Anyhow, this isn't a definitive answer, but maybe It'll save others time barking up the wrong tree.

提交回复
热议问题