import time # 引入time模块 ticks = time.time()获取当前时间分开形式(localtime):
import time localtime = time.localtime(time.time()) print ( localtime)
输出:
time.struct_time(tm_year=2021, tm_mon=11, tm_mday=4, tm_hour=21, tm_min=48, tm_sec=3, tm_wday=3, tm_yday=308, tm_isdst=0)
取值可以通过 localtime .tm_year方式来单独取值
获得详细时间(datetime.now)import datetime print(datetime.datetime.now())
输出
2021-11-04 21:46:15.944999字符串转时间类型(strptime)
import datetime formats="%s-%02d-%02d" % (year, mon,day) this_mon=datetime.datetime.strptime(formats,"%Y-%m-%d")时间类型转字符串(strptime)
import datetime localtimes=time.localtime() year=localtimes.tm_year mon=localtimes.tm_mon day=localtimes.tm_mday day_str="%s-%02d-%02d" % (year, mon,day) this_day=datetime.datetime.strptime(day_str,"%Y-%m-%d")时间类型减或加去多少天(timedelta)
今天减去一天:
import datetime 时间类型-datetime.timedelta(days=i)
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)