1, datetime general introduction to the library
(1) included in the class
-
DATE : Date Object
-
Time : Time Object
-
timedelta : Interval Object
-
datetime : Date Time Object
-
tzinfo : time zone information objects
(2) datetime constants contained in block
-
MaxYear : Returns the maximum year that can be represented, the return value 9999
-
MinYear : Returns the minimum year can be expressed, the return value is 1
2, date class
(1) date objects
date objects from the year , month The , Day three parts
date(year, month, day)
By year , month The , Day descriptor to access data in the three data
d = datetime.date(2020, 01, 14) >>>d.year 2020 >>>d.month 1 >>>d.day 14
Can also __getattribute __ () acquires the result of the process:
d = datetime.date(2020, 1, 14) >>>d.__getattribute__(year) 2020 >>>d.__getattribute__(month) 1 >>>d.__getattribute__(month) 14
(2) date object contains objects and attributes in
A method for comparing the size of the date
- __ __eq () : is equal to, for example: X __ .__ EQ (Y)
- __ __ge () : not less than, for example: X .__ GE __ (Y)
- __ __gt () : greater than, for example: X .__ gt __ (Y)
- __ __le () : less than or equal, for example: X .__ Le __ (Y)
- __ __lt () : less than, for example: X .__ lt __ (Y)
- __ __ne () : is not equal to, for example: X .__ NE __ (Y)
These methods return values: True / False
Gets the number of days between two dates
Use __sub __ () and __rsub __ () method, in fact, this method is almost two, is a forward operation, a reverse operation
- __ __sub () : Example: X .__ Sub __ (Y) , corresponding to xy
- __ __rsub () : Example: X .__ RSUB __ (Y) , corresponding to yx
(3) ISO standardized Date
Get in line with ISO date standard
- isocalendar () method: returns a tuple of three values of the tuple content (the year, where the first few weeks of years, where the first few days of the week)
>>>d = datetime.date(2020, 5, 14) >>>d.isocalendar() (2020, 20, 4)
-
isoformat () Method: Returns meet ISO 8601 standard ( YYYY the MM-DD- date strings)
>>>d = datetime.date(2020, 1, 14) >>>d.isoformat() 2020-01-14 <class 'str'>
- isoweekday () returns in line with ISO first few days of the date specified standard where the current date
>>>d = datetime.date(2020, 1, 14) >>>d.isoweekday() 2
(4) Other methods and properties
-
timetuple (): This method for compatibility time.localtime () Returns a type time.struct_time array, but the element of time thereof was 0
>>>d = datetime.date(2020, 1, 14) >>>d.timetuple() >>>time.struct_time(tm_year=2020, tm_mon=1, tm_mday=14, tm_hour=0, tm_min=0, tm_sec=0, tm_wday=1, tm_yday=14, tm_isdst=-1) >>>d.timetuple().tm_year 2020 >>>d.timetuple().tm_mon 1 >>>d.timetuple().tm_mday 14
- toordinal () : Returns the calendar year to begin several days now, AD 1 of 1 Yue 1 day 1
>>>d = datetime.date(2020, 1, 14) >>>d.toordinal() 737438
- the replace () : Returns a replacement date fields to develop new date object. . 3 optional parameter, respectively, year , month The , Day . Note that after replacing the heart to produce objects, without affecting the original object data
>>>d = datetime.date(2020, 1, 14) >>>a = d.replace(2020, 2, 14) >>>a 2020-02-14
-
fromtimestamp (): the given timestamp, returns a date objects
>>>datetime.date.fromtimestamp(time.time())
2020-01-14
-
Today () : Returns the current date
>>>datetime.date.today() >>>print(datetime.date.today()) 2020-01-14