Converting datetime to POSIX time

后端 未结 6 1959
忘了有多久
忘了有多久 2021-01-31 02:14

How do I convert a datetime or date object into a POSIX timestamp in python? There are methods to create a datetime object out of a timestamp, but I don\'t seem to find any obv

6条回答
  •  没有蜡笔的小新
    2021-01-31 02:35

    Note that Python now (3.5.2) includes a built-in method for this in datetime objects:

    >>> import datetime
    >>> now = datetime.datetime(2020, 11, 18, 18, 52, 47, 874766)
    >>> now.timestamp() # Local time
    1605743567.874766
    >>> now.replace(tzinfo=datetime.timezone.utc).timestamp() # UTC
    1605725567.874766 # 5 hours delta (I'm in UTC-5)
    

提交回复
热议问题