python - Python Datetime将所有值合并到int
问题描述
我想将程序的日期时间转换为 int,我想要的是:
import datetime
print(datetime.datetime.utcnow())
> 2021-01-12 09:15:16.9791000 # the time I ran the command
进入这个:
> 202101120915169791000
我怎样才能做到这一点?
解决方案
使用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
推荐阅读
- sql-server - SQL Server - 查询返回重复记录
- laravel - Laravel根据列值定义关系中的外键
- java - Fabric8 卡在停止并移除 docker 容器
- javascript - 当我关闭灯箱时,有没有办法不滚动回顶部?
- c - 在不知道数据长度的情况下读取在套接字中发送的数据块
- python-3.x - box-python-sdk is not returning total_count for Get Enterprise Users and Get Group
- mysql - Hive Metastore 每四到六个小时就会断开连接
- python - 向mysql插入大量行
- javascript - 通过样式化组件在 react-native 中定义数字样式属性(border-bottom-width、height、width 等)
- file - golang:监视(和读取)为写入而打开的文件