时间模块-time模块
1.时间戳 time>time() 从1970 至今过了多少秒 (时间戳、格式化时间、时间对象)
print(time.time())
2. 格式时间 time.strftime(%Y-%m-%d %H-%M-%S) str formate time 格式为字符串
''' %Y Year with century as a decimal number. %m Month as a decimal number [01,12]. %d Day of the month as a decimal number [01,31]. %H Hour (24-hour clock) as a decimal number [00,23]. %M Minute as a decimal number [00,59]. %S Second as a decimal number [00,61]. ''' print(time.strftime('%Y-%m-%d %H:%M:%S')) # %X == %H:%M:%S print(time.strftime('%Y-%m-%d %X')) print(type(time.strftime('%Y-%m-%d %X'))) # 获取年月 print(time.strftime('%Y/%m'))
3.获取时间对象 time.strptime(时间格式)、time.localtime() 当前时间对象
返回的是一个元组, 元组中有9个值:
9个值分别代表: 年、月、日、时、分、秒、一周中第几天,一年中的第几天,夏令时(了解)
# 3.获取时间对象 (****)
print(time.localtime()) print(type(time.localtime())) time_obj = time.localtime() print(time_obj.tm_year) print(time_obj.tm_year) print(time_obj.tm_mon) time.struct_time(tm_year=2019, tm_mon=11, tm_mday=17, tm_hour=21, tm_min=48, tm_sec=28, tm_wday=6, tm_yday=321, tm_isdst=0) <class 'time.struct_time'> 2019 2019 11
# res = time.strptime('2019-01-01', '%Y-%m-%d') time.strptime() 把格式化时间转化为对象
# print(res)
# print(res.tm_min)
3.把时间对象转化为格式化时间
# res = time.localtime() # time.sleep(5) # 获取当前时间的格式化时间 # print(time.strftime('%Y-%m-%d %H:%M:%S', time.localtime())) # # # 将时间对象转为格式化时间 # print(time.strftime('%Y-%m-%d %H:%M:%S', res))