Python日期时间将所有值合并到int中

Ric*_*lcm 2 python python-3.x python-datetime

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

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

> 2021-01-12 09:15:16.9791000 # the time I ran the command
Run Code Online (Sandbox Code Playgroud)

进入这个:

> 202101120915169791000
Run Code Online (Sandbox Code Playgroud)

我怎样才能做到这一点?

mhh*_*bib 8

使用strftime格式。

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

# Output
# 20210112092900
Run Code Online (Sandbox Code Playgroud)

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

print(int(a.strftime('%Y%m%d%H%M%S%f')))
# Output
#20210112092900275246
Run Code Online (Sandbox Code Playgroud)