【问题标题】:DateTime in ISO 8601 + TimeZone and without microsecondsISO 8601 + TimeZone 中的日期时间,没有微秒
【发布时间】:2023-04-02 15:45:02
【问题描述】:

我需要 ISO 8601 中没有任何微秒的日期时间字符串。

喜欢:

2015-01-05T11:26:00-03:00

我用:

from pytz import timezone
from datetime import datetime
timezone(settings.TIME_ZONE).localize(datetime.now()).isoformat()

但它会返回:

'2015-01-28T17:49:39.711725-03:00'

如何解决?

【问题讨论】:

标签: python django pytz rfc3339


【解决方案1】:

设置microsecond为0:

t = timezone(settings.TIME_ZONE).localize(datetime.now()).replace(microsecond=0)
t.isoformat()

datetime.datetime.replace(...) 将返回一个新的datetime 对象,并修改了指定的属性。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2010-10-23
    • 2015-01-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多