https://docs.python.org/3/library/datetime.html
1. Method for comparing the size of dates
method name | Method description | usage |
---|---|---|
__eq__(…) | equals(x==y) | x.__eq__(y) |
__give__(…) | Greater than or equal to (x>=y) | x.__ge__(y) |
__gt__(…) | Greater than (x>y) | x.__gt__(y) |
__the__(…) | Less than or equal to (x<=y) | x.__le__(y) |
__lt__(…) | less than (x | x.__lt__(y) |
__born__(…) | not equal (x!=y) | x.__ne__(y) |
2. Time format
1 Time and date formatting symbols in python: 2 %y two-digit year representation (00-99 ) 3 %Y four-digit year representation (000-9999 ) 4 %m month (01-12 ) 5 %d month day (0-31 ) 6 %H 24-hour hour (0-23 ) 7 %I 12-hour hour (01-12 ) 8 %M minute (00=59 ) 9 %S second ( 00-59 ) 10 % a Local abbreviated week name 11 % A Local full week name 12 % b Local abbreviated month name 13 % B Local full month name 14 % c Local corresponding date representation and time representation 15 % of the year in j One day (001-366 ) 16 % p Local AM or PM equivalent 17 %U Week number of the year (00-53 ) Sunday is the start of the week 18 %w Week (0-6 ), Sunday is the start of the week 19 %W The year The week number in (00-53 ) Monday is the start of the week 20 % x the local corresponding date represents 21 % X the local corresponding time represents 22 % Z the name of the current time zone 23 %% % number itself
3, datetime, date module
1 datetime.date 2 Attributes: year, month, and day. 3 4 datetime.time 5 Attributes: hour, minute, second, microsecond, and tzinfo. 6 7 datetime.datetime 8 Attributes: year, month, day, hour, minute, second, microsecond, and tzinfo. 9 10 11 datetime.date.today() #datetime.date(2018, 4, 26) 返回不精确的时间 12 datetime.date.strftime(datetime.date.today(),'%Y%m%d %H%M%S') #时间解析'20180426 000000' 13datetime.datetime.today() # datetime.datetime(2018, 4, 26, 20, 29, 25, 286001) returns the exact time 14 datetime.date.strftime(datetime.datetime.today(), ' %Y%m %d %H%M%S ' ) #Time analysis '20180426 203144' 15 #Interval time 16 res = datetime.datetime.today()+datetime.timedelta(days=1,minutes=5,seconds=5,weeks= 5 ) 17 print (res.strftime( ' %Y-%m-%d ' ))
4、time
1 print (time.time()) #Get the current timestamp 2 # time.sleep(10) 3 today = time.strftime( ' %Y-%m-%d %H:%M:%S ' ) 4 print (today) 5 6 print (time.gmtime()) #The default time is the time in the standard time zone 7 s=time.localtime(1514198608) #The time in the current time zone is obtained, which can be passed in seconds 8 print (time. strftime( ' %Y-%m-%d %H:%M:%S ' ,s)) 9 time.localtime() #Get the current timestamp 10 11 Result: 12 1525507887.303 13 2018-05-05 16:11:27 14 time.struct_time(tm_year=2018, tm_mon=5, tm_mday=5, tm_hour=8, tm_min=11, tm_sec=27, tm_wday=5, tm_yday=125, tm_isdst=0) 15 2017-12-25 18:43:28