gpt4 book ai didi

Python - datetime.now() 返回不正确的时间

转载 作者:行者123 更新时间:2023-12-01 09:05:24 25 4
gpt4 key购买 nike

我想在我的数据库中存储操作的时间戳。问题是我从 datetime.datetime.now() 获得的时间不正确(我猜这是由于时区造成的)。我尝试使用 python 获取终端中的值并得到了正确的结果。

import datetime 
datetime.datetime.now()//correct time

但是,如果我在问题执行期间打印结果,则输出不正确,结果会提前两个小时。

为什么会这样,终端给了我正确的时间,但程序实例却没有,我怎样才能在程序中获得正确的时间?

编辑:来自终端的日期时间:

2018-08-30 17:41:04.413187

程序实例的日期时间:

2018-08-30 14:42:31.761310

编辑:

我注意到运行程序时出现此警告。

RuntimeWarning: DateTimeField Policy.last_update received a naive datetime (2018-08-30 15:16:23.689896) while time zone support is active.
RuntimeWarning)

最佳答案

尝试根据您所在的时区调整小时数:

import datetime 

datetime.datetime.utcnow()+datetime.timedelta(hours=3)

关于Python - datetime.now() 返回不正确的时间,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52099464/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com