Python - simplest and most coherent way to get timezone-aware current time in UTC?

前端 未结 3 1071
清歌不尽
清歌不尽 2020-12-10 23:10

datetime.now() doesn\'t appear to have timezone info attached. I want the current time in UTC. What do I do?

>>> from datetime import         


        
3条回答
  •  情深已故
    2020-12-10 23:47

    Use datetime.utcnow() for the current time in UTC.

    Adapting from this answer, here is how to make the object timezone-aware.

    >>> import pytz
    >>> from datetime import datetime
    >>> datetime.now(pytz.utc)
    datetime.datetime(2014, 3, 11, 15, 34, 52, 229959, tzinfo=)
    

提交回复
热议问题