Understanding time:
1, the timestamp Time, GMT, float data type used to machine
1970.1.1 London Time 0: 0: 0
GMT 8 1970.1.1: 0: 0
is the initial number of seconds counted
2, the structure of the time, the time the object
time of the object can be acquired by the property name of the object. value '
3, formatting time, string time, str data type posters
may be displayed according to the format of the time you need to
obtain three types of time
Print (time.time ()) # timestamp time 1586179692.631992 Print (The time.strftime ( ' %% Y-M-% D ' )) # formatting time str format time 2020-04-06
time_obj = time.localtime() # 对象数据结构的 print(time_obj) #time.struct_time(tm_year=2020, tm_mon=4, tm_mday=6, tm_hour=21, tm_min=28, tm_sec=12, tm_wday=0, tm_yday=97, tm_isdst=0) print(time_obj.tm_year) #2020 print(time_obj.tm_mday # 6
% y represents a two-digit year (00-99 ) % the Y represents a four-digit year (000-9999 ) % m (01-12 ) within% d month of day (0-31 of ) % H 24 hours number (manufactured by 0-23 hours ) % h 12 is the number of the I (manufactured by 01-12 hours ) % M number of minutes (00 = 59 ) % S seconds (00-59 ) % A simplified week local name % A full week of local name % b local simplified month name % B full month name local % C indicates the corresponding local date and time indicates one day (001-366% j years ) % P AM or PM local character equivalent % of U week of the year number (00-53 ) on Sunday as the start of the week % w week (0-6 ), Sunday to begin the week of % W week number of the year (00-53 ) Monday to start the week of % the X-local corresponding date It represents % X-indicates the corresponding local time % Name Z current time zone number %%% itself
Index (Index) property (the Attribute) values (Values) 0 tm_year (years), such as 2011 1 tm_mon (month) 1 - 12 2 tm_mday (Japan) 1--31 3 tm_hour(时) 0 - 23 4 tm_min (min) 0 - 59 5 tm_sec (sec) 0--60 Tm_wday 6 (WEEKDAY) 0 - 6 (0 expressed Monday) (the first day of the year) 7 tm_yday 1 - 366 8 tm_isdst (whether it is daylight saving time) default is 0
print(time.strftime('%Y-%m-%d %A %H:%M:%S')) # 重要 print(time.strftime('%y-%m-%d %A %H:%M:%S')) print(time.strftime('%y/%m/%d %H:%M:%S')) print(time.strftime('%c')) —————————————————— 2020-04-06 Monday 21:43:38 20-04-06 Monday 21:43:38 20/04/06 9:43:38 p.m. Mon Apr 6, 2020 9:43:38 p.m.
Three Time conversion:
1, the time stamp acquired is converted into a structured time, the event time object is converted into formatted
print(time.time()) print(time.localtime(1500000000)) print(time.localtime(2000000000)) time_obj = time.localtime(3000000000) format_time = time.strftime('%y-%m-%d %H:%M:%S',time_obj) print(format_time)
2, acquisition time format, is converted into a structured time, the event object into a timestamp Time
struct_time = time.strptime('2008-8-8','%Y-%m-%d') print(struct_time) print(time.mktime(struct_time))
Small scale chopper:
1, calculate the timestamp 1st time this month
struct_time = time.localtime() struct_time = time.strptime('%s-%s-1' % (struct_time.tm_year,struct_time.tm_mon), '%Y-%m-%d') print(time.mktime(struct_time)) # First obtaining a structured time, and then acquire the properties of formatted time struct_time structured time, obtaining a structured One month time, and then into a timestamp mktime
ret = time.strftime('%Y-%m-1') struct_time = time.strptime(ret,'%Y-%m-%d') print(time.mktime(struct_time)) # First get formatted time (No. 1-coded), with strptime converted into a structured time, and finally converted to a timestamp with mktime