首页 > 解决方案 > Python Datetime将所有值合并到int

问题描述

我想将程序的日期时间转换为 int,我想要的是:

import datetime
print(datetime.datetime.utcnow())

> 2021-01-12 09:15:16.9791000 # the time I ran the command

进入这个:

> 202101120915169791000

我怎样才能做到这一点?

标签: pythonpython-3.xpython-datetime

解决方案


使用strftime格式。

import datetime
a = datetime.datetime.utcnow()
a = int(a.strftime('%Y%m%d%H%M%S'))
print(a)

# Output
# 20210112092900

如果您想获得整个纳秒,请尝试

print(int(a.strftime('%Y%m%d%H%M%S%f')))
# Output
#20210112092900275246

推荐阅读