python time 模块

此时此刻时间浮点数

In [58]: seconds = time.time()In [60]: secondsOut[60]: 1582341559.0950701

时间数组

In [61]: local_time = time.localtime(seconds)In [62]: local_timeOut[62]: time.struct_time(tm_year=2020, tm_mon=2, tm_mday=22, tm_hour=11, tm_min=19, tm_sec=19, tm_wday=5, tm_yday=53, tm_isdst=0)

时间字符串

time.asctime 语义:as convert time

In [63]: str_time = time.asctime(local_time)In [64]: str_timeOut[64]: 'Sat Feb 22 11:19:19 2020'

格式化时间字符串

time.strftime 语义:string format time

In [65]: format_time = time.strftime('%Y-%m-%d %H:%M:%S',local_time)In [66]: format_timeOut[66]: '2020-02-22 11:19:19'

字符时间转时间数组

In [68]: str_to_struct = time.strptime(format_time,'%Y-%m-%d %H:%M:%S')In [69]: str_to_structOut[69]: time.struct_time(tm_year=2020, tm_mon=2, tm_mday=22, tm_hour=11, tm_min=19, tm_sec=19, tm_wday=5, tm_yday=53, tm_isdst=-1)

常用字符串格式

  • %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].
  • %z Time zone offset from UTC.
  • %a Locale's abbreviated weekday name.
  • %A Locale's full weekday name.
  • %b Locale's abbreviated month name.