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&rsquo ;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中文網其他相關文章!