获取当前时间:
date = datetime.datetime.now() #date: 2019-09-26 14:26:09.025580
date1 = time.strftime('%Y%m%d:%H%M%S',time.localtime()) #'20231114:203857' type(str)
date2 = time.strftime('%Y%m%d:%H%M%S',time.localtime(time.time())) #'20231114:203857' type(str)
获取当天零点时刻:
end_date = datetime.datetime.strptime(str(date.year)+'-'+str(date.month)+'-'+str(date.day) + ' 00:00:00', "%Y-%m-%d %H:%M:%S")
#date: 2019-09-26 00:00:00
字符串2时间数组:
dt = '2016-01-01 00:00:00' #dt: '2016-01-01 00:00:00'
timeArray = time.strptime(dt, "%Y-%m-%d %H:%M:%S")
#timeArray: time.struct_time(tm_year=2016, tm_mon=1, tm_mday=1, tm_hour=0, tm_min=0, tm_sec=0, tm_wday=4, tm_yday=1, tm_isdst=-1)
时间数组2时间戳:
timestamp = time.mktime(timeArray) #timestamp: 1451577600.0
时间戳2时间数组:
time_local = time.localtime(timestamp)
#time_local: time.struct_time(tm_year=2016, tm_mon=1, tm_mday=1, tm_hour=0, tm_min=0, tm_sec=0, tm_wday=4, tm_yday=1, tm_isdst=0)
时间数组2字符串:
date = time.strftime("%Y-%m-%d %H:%M:%S",time_local) #date:'2016-01-01 00:00:00'