1、time.time()
importtime
t1=time.time()print(t1) #1594006474.1072185
2、获取当地时间-->结构化时间 time.localtime()
importtime
t2=time.localtime()print(t2)#time.struct_time(tm_year=2020, tm_mon=7, tm_mday=1, tm_hour=0, tm_min=0, tm_sec=0, tm_wday=2, tm_yday=183, tm_isdst=-1)
3、根据指定格式,获取结构化时间 time.strptime()
importtime
t2= time.strptime("30 11 2000", "%d %m %Y")print(t2)#time.struct_time(tm_year=2000, tm_mon=11, tm_mday=30, tm_hour=0, tm_min=0, tm_sec=0, tm_wday=3, tm_yday=335, 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.
%B Locale's full month name.
%c Locale's appropriate date and time representation.
%I Hour (12-hour clock) as a decimal number [01,12].
%p Locale's equivalent of either AM or PM.
importtime
t2= time.strptime("2020-07-01", "%Y{y}%m{y}%d".format(y="-"))print(t2)#time.struct_time(tm_year=2020, tm_mon=7, tm_mday=1, tm_hour=0, tm_min=0, tm_sec=0, tm_wday=2, tm_yday=183, tm_isdst=-1)
4、根据结构时间,获取指定格式时间 time.strftime()
importtime
t2= time.localtime() #time.struct_time(tm_year=2020, tm_mon=7, tm_mday=1, tm_hour=0, tm_min=0, tm_sec=0, tm_wday=2, tm_yday=183, tm_isdst=-1)
t3= time.strftime("%Y{y}%m{y}%d".format(y="-"),t2)print(t3)#2020-07-01
5、根据结构时间,获取时间戳 time.mktime()
importtime
t2= time.localtime() #time.struct_time(tm_year=2020, tm_mon=7, tm_mday=1, tm_hour=0, tm_min=0, tm_sec=0, tm_wday=2, tm_yday=183, tm_isdst=-1)
t4=time.mktime(t2)print(t4)#1593532800.0
6、补充datetime用法。
7、date用法
from datetime importdate,timedelta
date_today=date.today()print(date_today) #2020-07-11
from datetime importdate,timedelta#date_today = date.today()
#print(date_today) # 2020-07-11
t1= timedelta(days=7)print(t1) #7 days, 0:00:00
from datetime importdate,timedelta,datetime#date_today = date.today()
#print(date_today) # 2020-07-11
#t1 = timedelta(days=7)#
#print(t1) # 7 days, 0:00:00
t2= datetime.strptime("2020-07-01", '%Y-%m-%d')print(t2) #2020-07-01 00:00:00
最后出现神奇的一幕。
from datetime importdate,timedelta,datetime#date_today = date.today()
#print(date_today) # 2020-07-11
#t1 = timedelta(days=7)#
#print(t1) # 7 days, 0:00:00
t2= datetime.strptime("2020-07-01", '%Y-%m-%d')#
#print(t2) # 2020-07-01 00:00:00
t3= timedelta(days=7)print(t3) #7 days, 0:00:00
t4= t2 -t3print(t4) #2020-06-24 00:00:00