python时间相关

time

time — Time access and conversions — Python 3.10.0 documentation

import time

# 1970-01-01 00:00:00 到现在经过了多少秒
print(time.time())

# 返回一个时间元组
print(time.localtime())

# 时间转字符串
print(time.strftime('%Y-%m-%d %H:%M:%S',time.localtime()))

# 字符串转时间
print(time.strptime('2021-10-15 15:25:04','%Y-%m-%d %H:%M:%S'))

datetime

datetime — Basic date and time types — Python 3.10.0 documentation

from datetime import datetime

# 当前时间
print(datetime.now())

# 时间转字符串
print(datetime.now().strftime('%Y-%m-%d %H:%M:%S'))

# 字符串转时间
print(datetime.strptime('2017-3-22 15:25:01','%Y-%m-%d %H:%M:%S'))

参考博客

python与时间相关的库_szuhuanggang的博客-CSDN博客

python——time库整理(基础知识+实例) - 知乎 (zhihu.com)

Python datetime模块详解、示例_Steven的博客-CSDN博客_python中的datetime

Python中time模块详解_You_are_my_dream的博客-CSDN博客

python datetime与timestamp_dudu3332的博客-CSDN博客

posted @ 2023-05-08 16:20  张浩东zhanghad  阅读(8)  评论(0编辑  收藏  举报