Python's Time library can perform time-related processing, such as accessing the current date and time, outputting time in different formats, and waiting for a specified time.
import time timestamp = time.time() # 1682737552.5009851
Starting from 00:00:00 on January 1, 1970, Greenwich Mean Time (GMT) The total number of seconds to date
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.(year)
%m Month as a decimal number [01,12].(month)
%d Day of the month as a decimal number [01,31].(日)
% H Hour (24-hour clock) as a decimal number [00,23].(hour-24 hour)
%M Minute as a decimal number [00,59].(minute)
%S Second as a decimal number [00,61].(seconds)
%z Time zone offset from UTC.(The time difference between international standard time and local time (East Eighth District, Beijing time))
%a Locale’s abbreviated weekday name.(Day of the week-abbreviation)
%A Locale’s full weekday name.(Day of the week-full name)
%b Locale’s abbreviated month name.(Month name-abbreviation)
%B Locale&rsquo ;s full month name.(month name-full name)
%c Locale’s appropriate date and time representation.(linux time format)
%I Hour (12-hour clock) as a decimal number [01 ,12].(hour-12 o'clock)
%p Locale’s equivalent of either AM or PM.(morning or afternoon)
%X hour:minute:second, and %H:%M:%S The effect is the same
%x hours/minutes/seconds
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)
The above is the detailed content of How to obtain and convert time in Python time module. For more information, please follow other related articles on the PHP Chinese website!