python中处理时间的模块
datetime 模块常用的类
- datetime(from datetime import datetime) 时间日期相关
- timedelta(from datetime import timedelta) 计算两个时间的时间差
- timezone(from datetime import timezone) 时区相关
import datetime
nowtime = datetime.datetime.now()
print(nowtime.day)
print(nowtime.month)
print(nowtime.year)
print(nowtime.hour)
t = nowtime.timestamp()
s = datetime.datetime.fromtimestamp(t)
datetime.datetime(2022,10,10)
s= "2022-04-11 10:40:46"
s1 = datetime.datetime.strptime(s, '%Y-%m-%d %H:%M:%S')
nowtime = datetime.datetime.now()
s1 = nowtime.strftime('%Y-%m-%d %H:%M:%S')
print(type(s1), s1)
nowtime.timestamp() :datetime -----> 时间戳 datetime.datetime.fromtimestamp(时间戳) :时间戳-----> datetime
datetime.datetime.strptime(字符串, '%Y-%m-%d %H:%M:%S') :字符串-----> datetime nowtime.strftime('%Y-%m-%d %H:%M:%S') :datetime-----> 字符串
|