Introduced
The Datetime module is the re-encapsulation of the time module, providing more interfaces. The main is the date and time of parsing, formatting and operation.
Other time-related modules:
Time-basic
Calendar-basic
Pytz-About time zones
Dateutil-extension of datetime
Common Operation Times Class
ImportDatetimet= Datetime.time (+/-)#01:02:03Print(t)#syntax: Datetime.time (HOUR,MIN,SEC)Print("Hour:", T.hour)#hour:1Print('minute:', T.minute)#Minute:2Print('Second:', T.second)Print('microsecond:', T.microsecond)Print('Tzinfo:', T.tzinfo)#Time Zone InfoPrint('earlist:', Datetime.time.min)#earlist:00:00:00Print('Latest:', Datetime.time.max)#latest:23:59:59.999999Print('Resolution:', datetime.time.resolution)#resolution:0:00:00.000001
Note that microsecond milliseconds cannot be expressed in float mode, only integers.
Dates class
The date class is primarily used to represent calendar date. The attributes include year, month, and day.
ImportDatetimetoday=Datetime.date.today ()Print(today)#2017-08-20Print('CTime:', Today.ctime ())#Ctime:sun 00:00:00Print('ordinal:', Today.toordinal ())#ordinal:736561Print("Year :", Today.year)#year:2017Print('Month:', Today.month)#Month:8Print('Day :', Today.day)#day:20Tuple_today=today.timetuple ()Print(Tuple_today)#time.struct_time (tm_year=2017, tm_mon=8, tm_mday=20, tm_hour=0, tm_min=0, tm_sec=0, tm_wday=6, tm_yday=232, tm_ Isdst=-1)Print(tuple_today.tm_year)#extract the elements in the Struct_time
Timedeltas
Date arithmetic
Comparing values
Combining dates and times
Formatting and parsing
Time zones
Python Basics-Day 5 learning note-Standard library of modules: DateTime (2)