datetime type commonly used content
Code
from datetime import datetime from datetime import date import time #datetime()和time() """ datetime common module types: date: date, common properties: year, month, day time: time datetime: date and time, common attributes: hour, minute, second, microsecond timedelta: the length of the time interval between two time points datetime module constants: MAXYEAR, datetime.MAXYEAR maximum year MINYEAR, datetime.MINYEAR year minimum """ # (A) Class DATE TD = datetime.today () Print ( " Today IS " , TD) # acquires the current date, time: 2019-11-2420: 17: 28.933784 # Get the current date a method Print ( " Today's year IS " , td.year) # current Year: 2019 Print ( " Today's month The IS " , td.month) # the current month: 11 Print ( " Today's day IS " , td.day) # the current day: 24 # Method for obtaining the current year, month, day two: .__ getattribute __ () method Print ( " 20,191,102's year IS " , td. __Getattribute__ ( " year " )) # Current Year: 2019 Print ( " 20,191,102's month The IS " ., Td __getattribute__ ( " month The " )) # the current month: 11 Print ( " 20,191,102 IS's day " ., td __getattribute__ ( " day " )) # the current day: 24 # .Fromtimestamp () method, by a given timestamp, returns the corresponding date object Print (datetime.fromtimestamp (the time.time ())) # 2019-11-25 06: 59: 23.325629 Print (datetime.utcfromtimestamp (Time .time ())) # 2019-11-24 22 is: 59: 23.325629 difference 8 hours Print (date.fromtimestamp (the time.time ())) # 2019-11-25 print (datetime.now()) #2019-11-25 06:59:23.325629 print (datetime.now().date()) #2019-11-25 print (datetime.now().time()) #06:59:23.325629 print (datetime.utcnow()) #2019-11-24 22:59:23.325629 差8小时 print (time.time()) #1574636363.3256292 print (datetime.today()) #2019-11-25 07:03:10.692589 print (datetime.today().date()) #2019-11-25 print (date.today()) #2019-11-25 "" " Method is used to compare the size of date: Illustrate the use of the method name method __eq __ (...) is equal to (x == y) x .__ eq __ (y) __ge __ (...) greater than or equal (x> = y) x .__ ge __ (y) __gt __ (...) is larger than (x> y) x .__ gt __ (y) __le __ (...) or less (x <= y) x .__ le __ (y) __lt__(…) 大于(x<y) x.__lt__(y) __ne __ (...) is not equal to (x! = y) x .__ ne __ (y) __sub__(…) x - y x.__sub__(y) __rsub __ (...) and - x x .__ __ rsub (y) """ a=date(2017,3,1) b=date(2017,3,15) print("a=b,a.__eq__(b)",a.__eq__(b)) # print("a≥b,a.__ge__(b)",a.__ge__(b)) # print("a>b,a.__gt__(b)",a.__gt__(b)) # print("a≤b,a.__le__(b)",a.__le__(b)) # print("a<b,a.__lt__(b)",a.__lt__(b)) # print("a!=b,a.__ne__(b)",a.__ne__(b)) # print("a-b,.__sub__(b)",a.__sub__(b)) # -14 days, 0:00:00 print("b-a,a.__rsub__(b)",a.__rsub__(b)) # 14 days, 0:00:00 print("a-b,.__sub__(b)",a.__sub__(b).days) #14 (days obtain a phase difference, using .days) Print ( " BA, A .__ RSUB __ (B) " , A. __Rsub__ (B) .days) # 14 (number of days difference acquired using .days) """ ISO-compliant date, three methods: 1, isocalendar (...): returns a tuple containing three values were: year year, week number of weeks, weekday weeks (Monday as 1 ... Sunday, 7): 2, isoformat (...): Returns the date strings conform to the ISO 8601 standard (YYYY-MM-DD) of 3, isoweekday (...): Returns the specified date in accordance with ISO standards where the number of weeks (Monday as 1 ... Sunday 7) "" " Print (a.isocalendar ()) # (2017,. 9,. 3) Print (a.isocalendar () [0]) # acquired in Print (a.isocalendar () [. 1]) # Get the number of weeks Print ( a.isocalendar () [2]) # Gets week Print (a.isoformat ()) # 2017-03-01 Print (a.isoweekday ()) # get the number of weeks Print (a.weekday ()) # Gets week, Monday is 0 """ Date Format: date into str, date display into a string __format __ (...) method of outputting the date (and this method is equivalent to the strftime method (...)) to specify the format, or using .__ str __ () method """ print (a.__str__()) #2017-03-01 print (a.__format__("%Y-%m-%d")) #2017-03-01 print (a.strftime("%Y-%m-%d")) #2017-03-01 Print (A. __format__ ( " % the Y /% m /% D " )) # 2017/03/01 (uppercase the Y) Print (a.strftime ( " % the Y /% m /% D " )) # 2017 / 03/01 (uppercase Y) Print (A. __format__ ( " % D% the Y% m " )) # 20,170,301 (uppercase Y) Print (a.strftime ( " % D% the Y% m " )) # 20,170,301 (uppercase Y) Print (A. __format__ ( " % Y /% m /% D " )) # 17/03/01 (lowercase Y) Print (a.strftime ( " % Y /% m /% D " )) # . 17 / 03/01 (lowercase y) print (a.__format__("%D")) #03/01/17 print (a.strftime("%D")) #03/01/17 # Date exercises; calculating difference date, a method: direct Save SUM = 0 dates = ['2019-1-2','2019-1-27','2019-2-25','2019-3-25','2019-4-22','2019-5-20','2019-6-16','2019-7-19','2019-8-15','2019-9-10','2019-10-9','2019-10-31','2019-11-24'] for i in range(len(dates)-1): dateq = datetime.strptime (a dates [I], ' %% Y-M-% D ' ) .date () # string format into date format dateh = datetime.strptime (a dates [I +. 1], ' % the Y - M-% D% ' ) .date () # string format into date format Print (dateq, dateh, End = ' ' ) # , End =' 'does not wrap, a box end of the line display datec = dateh - dateq # difference between the two dates Print ( ' separated: ' , datec.days) SUM + = datec.days Print ( " Total separated: " , SUM, " number: " , len (a dates) -1 ) Print ( " average total apart:.%. 2F " % (SUM / (len (a dates) -1 ))) # Date exercises; calculating difference date, two: __sub __ () method SUM = 0 dates = ['2019-1-2','2019-1-27','2019-2-25','2019-3-25','2019-4-22','2019-5-20','2019-6-16','2019-7-19','2019-8-15','2019-9-10','2019-10-9','2019-10-31','2019-11-24'] for i in range(len(dates)-1): dateq = datetime.strptime (a dates [I], ' %% Y-M-% D ' ) .date () # string format into date format dateh = datetime.strptime (a dates [I +. 1], ' % the Y - M-% D% ' ) .date () # string format into date format Print (dateq, dateh, End = ' ' ) # , End =' 'does not wrap, a box end of the line display datec = dateh. __sub__ (dateq) # difference between the two dates Print ( ' separated: ' , datec.days) SUM + = datec.days Print ( " Total separated: " , SUM, " number: " , len (a dates) -1 ) Print ( " average total apart:.%. 2F " % (SUM / (len (a dates) -1 ))) """ # Common errors: 问题:TypeError: descriptor 'date' requires a 'datetime.datetime' object but received a 'int' Original content: from datetime import date a = datetime.date(2017,3,1) The reason: When you import the module, the module is imported from a datetime date, so the date when writing class, you can write directly, without adding datetime Modified content: from datetime import date a = date(2017,3,1) or import datetime a = datetime.date(2017,3,1) """
result
Today is 2019-11-25 07:22:49.569223 Today's year is 2019 Today's month is 11 Today's day is 25 20191102's year is 2019 20191102's month is 11 20191102's day is 25 2019-11-25 07:22:49.569223 2019-11-24 23:22:49.569223 2019-11-25 2019-11-25 07:22:49.569223 2019-11-25 07:22:49.569223 2019-11-24 23:22:49.569223 1574637769.5692234 2019-11-25 07:22:49.569223 2019-11-25 2019-11-25 a=b,a.__eq__(b) False a≥b,a.__ge__(b) False a>b,a.__gt__(b) False a≤b,a.__le__(b) True a<b,a.__lt__(b) True a!=b,a.__ne__(b) True a-b,.__sub__(b) -14 days, 0:00:00 b-a,a.__rsub__(b) 14 days, 0:00:00 a-b,.__sub__(b) -14 b-a,a.__rsub__(b) 14 (2017, 9, 3) 2017 9 3 2017-03-01 3 2 2017-03-01 2017-03-01 2017-03-01 2017/03/01 2017/03/01 20170301 20170301 17/03/01 17/03/01 03/01/17 03/01/17 2019-01-02 2019-01-27 apart: 25 2019-01-27 2019-02-25 apart: 29 2019-02-25 2019-03-25 apart: 28 2019-03-25 2019-04-22 apart: 28 2019-04-22 2019-05-20 apart: 28 2019-05-20 2019-06-16 apart: 27 2019-06-16 2019-07-19 apart: 33 2019-07-19 2019-08-15 apart: 27 2019-08-15 2019-09-10 apart: 26 2019-09-10 2019-10-09 apart: 29 2019-10-09 2019-10-31 apart: 22 2019-10-31 2019-11-24 apart: 24 Total apart: 326: 12 Average spaced Total: 27.17 2019-01-02 2019-01-27 apart: 25 2019-01-27 2019-02-25 apart: 29 2019-02-25 2019-03-25 apart: 28 2019-03-25 2019-04-22 apart: 28 2019-04-22 2019-05-20 apart: 28 2019-05-20 2019-06-16 apart: 27 2019-06-16 2019-07-19 apart: 33 2019-07-19 2019-08-15 apart: 27 2019-08-15 2019-09-10 apart: 26 2019-09-10 2019-10-09 apart: 29 2019-10-09 2019-10-31 apart: 22 2019-10-31 2019-11-24 apart: 24 Total apart: 326: 12 Average spaced Total: 27.17