Python的Time库可以进行时间相关的处理,如访问当前日期和时间,输出不同格式的时间以及等待指定的时间等。
import time timestamp = time.time() # 1682737552.5009851
格林威治时间(GMT)1970年01月01日00时00分00秒起至现在的总秒数
import time struct_time = time.localtime() #time.struct_time(tm_year=2023, tm_mon=4, tm_mday=29, tm_hour=11, tm_min=6, tm_sec=43, tm_wday=6, tm_yday=120, tm_isdst=0)
import time format_time = time.strftime('%Y-%m-%d %H:%M:%S %p') # 2023-04-29 11:07:30 AM
%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].(时-24时)
%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.(月份名称-简写)
%B Locale’s full month name.(月份名称-全称)
%c Locale’s appropriate date and time representation.(linux的时间格式)
%I Hour (12-hour clock) as a decimal number [01,12].(时-12时)
%p Locale’s equivalent of either AM or PM.(上午 or 下午)
%X 时:分:秒,与%H:%M:%S效果相同
%x 时/分/秒
import time timestamp = time.time() # 1682738174.6577663 # 时间戳 > 结构化时间 struct_time = time.localtime(timestamp) # 默认time.time() # time.struct_time(tm_year=2023, tm_mon=4, tm_mday=29, tm_hour=11, tm_min=16, tm_sec=14, tm_wday=6, tm_yday=120, tm_isdst=0) # 结构化时间 > 时间戳 timestamp = time.mktime(struct_time) # 1682738174.0
import time struct_time = time.localtime() #time.struct_time(tm_year=2023, tm_mon=4, tm_mday=29, tm_hour=11, tm_min=21, tm_sec=43, tm_wday=6, tm_yday=120, tm_isdst=0) # 结构化时间 > 格式化时间 format_time = time.strftime('%Y-%m-%d %H:%M:%S', struct_time) #2023-04-29 11:21:43 # 格式化时间 > 结构化时间 struct_time = time.strptime(format_time, '%Y-%m-%d %H:%M:%S') #time.struct_time(tm_year=2023, tm_mon=4, tm_mday=29, tm_hour=11, tm_min=21, tm_sec=43, tm_wday=6, tm_yday=120, tm_isdst=-1)
以上是Python time模块时间怎么获取和转换的详细内容。更多信息请关注PHP中文网其他相关文章!