Why does datetime.datetime.utcnow() not contain timezone information?

前端 未结 9 1040
南方客
南方客 2020-11-29 16:12
datetime.datetime.utcnow()

Why does this datetime not have any timezone info given that it is explicitly a UTC datetime?<

相关标签:
9条回答
  • 2020-11-29 17:15

    To add timezone information in Python 3.2+

    import datetime
    
    >>> d = datetime.datetime.now(tz=datetime.timezone.utc)
    >>> print(d.tzinfo)
    'UTC+00:00'
    
    0 讨论(0)
  • 2020-11-29 17:16
    from datetime import datetime 
    from dateutil.relativedelta import relativedelta
    d = datetime.now()
    date = datetime.isoformat(d).split('.')[0]
    d_month = datetime.today() + relativedelta(months=1)
    next_month = datetime.isoformat(d_month).split('.')[0]
    
    0 讨论(0)
  • 2020-11-29 17:16

    UTC dates don't need any timezone info since they're UTC, which by definition means that they have no offset.

    0 讨论(0)
提交回复
热议问题