赞
踩
# str转struct_time import time cur_time = "2022-03-12 00:00:00" time_format = "%Y-%m-%d %H:%M:%S" cur_time = time.strptime(cur_time, time_format) print(cur_time) # time.struct_time(tm_year=2022, tm_mon=3, tm_mday=12, tm_hour=0, tm_min=0, tm_sec=0, tm_wday=5, tm_yday=71, tm_isdst=-1) # <class 'time.struct_time'> # struct_time转str cur_time = time.strftime(time_format, cur_time) print(type(cur_time)) # <class 'str'> # str转datetime from datetime import datetime cur_time = datetime.strptime(cur_time, time_format) print(type(cur_time)) # <class 'datetime.datetime'> # datetime转str cur_time = cur_time.strftime(time_format) print(type(cur_time)) # <class 'str'> # struct_time转datetime import datetime # str转struct_time cur_time = time.strptime(cur_time, time_format) cur_time = datetime.datetime(*cur_time[:6]) print(type(cur_time)) # <class 'datetime.datetime'> # struct_time转datetime from time import mktime cur_time = cur_time.strftime(time_format) print(type(cur_time)) cur_time = time.strptime(cur_time, time_format) print(type(cur_time)) cur_time = datetime.fromtimestamp(mktime(cur_time)) print(type(cur_time)) # <class 'datetime.datetime'> """ struct_time可以比较时间大小 datetime可以加减时间 datetime.timedelta(days=1) """
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。