6.Python中的time和datetime模块

    科技2024-10-06  18

    time模块:提供各种函数来操作时间值 datetime模块:提供各种函数来操作日期值

    时间转换过程

    import time import datetime #1602069408.981 timeStamp1=time.time() print(timeStamp1) #time.struct_time(tm_year=2020, tm_mon=10, tm_mday=7, tm_hour=19, tm_min=16, tm_sec=48, tm_wday=2, tm_yday=281, tm_isdst=0) struct_time1=time.localtime(timeStamp1) print(struct_time1) struct_time2=time.gmtime(timeStamp1) print(struct_time2) #2020-10-07 19:16:48 print(time.strftime('%Y-%m-%d %H:%M:%S',struct_time1)) #time.struct_time(tm_year=2020, tm_mon=10, tm_mday=7, tm_hour=19, tm_min=16, tm_sec=48, tm_wday=2, tm_yday=281, tm_isdst=-1) now='2020-10-07 19:16:48' struct_time3=time.strptime(now,'%Y-%m-%d %H:%M:%S') print(struct_time3) #1602069408.0 timeStamp2=time.mktime(struct_time3) print(timeStamp2) #2020-10-07 19:16:48.615000 now=datetime.datetime.now() print(now) #2020-10-07 19:16:48 print(now.strftime('%Y-%m-%d %H:%M:%S')) #2020-10-07 print(datetime.date.fromtimestamp(time.time())) #获得3天前的时间 2020-10-04 19:16:48 threeDayAgo=datetime.datetime.now()-datetime.timedelta(days=3) print(threeDayAgo)
    Processed: 0.046, SQL: 8