9 datetime
Standard Library
This is a built-in library by Python. There is no need to install this library.
9.1 ISO8601
https://en.wikipedia.org/wiki/ISO_8601#Time_zone_designators
9.2 Module Import
from datetime import date # module for date object
from datetime import time # module for time object
from datetime import datetime # module for datetime object
from datetime import timedelta
9.3 Class
datetime library contain three class of objects:
- date (year,month,day)
- time (hour,minute,second)
- datetime (year,month,day,hour,minute,second)
- timedelta: duration between two datetime or date object
9.4 date
9.4.1 Constructor
print( date(2000,1,1) )
#:> 2000-01-01
print( date(year=2000,month=1,day=1) )
#:> 2000-01-01
print( type(date(year=2000,month=1,day=1)))
#:> <class 'datetime.date'>
9.4.2 Class Method
9.4.3 Instance Method
9.4.3.1 replace()
- Replace year/month/day with specified parameter, non specified params will remain unchange.
- Example below change only month. You can change year or day in combination
print( date.today() )
#:> 2020-11-20
print( date.today().replace(month=8) )
#:> 2020-08-20
9.4.3.2 weekday(), isoweekday()
For weekday()
, Zero being Monday
For isoweekday()
, Zero being Sunday
print( date.today().weekday() )
#:> 4
print( date.today().isoweekday() )
#:> 5
= ['Mon','Tue','Wed','Thu','Fri','Sat','Sun']
weekdays = date.today().weekday()
wd print( date.today(), "is day", wd ,"which is", weekdays[wd] )
#:> 2020-11-20 is day 4 which is Fri
9.4.3.3 Formating with isoformat()
isoformat()
return ISO 8601 String (YYYY-MM-DD)
# return string date.today().isoformat()
#:> '2020-11-20'
9.4.3.4 Formating with strftime
For complete directive, see below:
https://docs.python.org/3/library/datetime.html#strftime-strptime-behavior
"%m/%d") date.today().strftime(
#:> '11/20'
9.5 date and datetime
9.5.1 Constructor
import datetime as dt
print(
2000,1,1,), '\n',
dt.date(2000,1,1,0,0,0), '\n',
dt.datetime(=2000,month=1,day=1,hour=23,minute=15,second=55),'\n',
dt.datetime(yeartype(dt.date(2000,1,1)),'\n',
type(dt.datetime(2000,1,1,0,0,0)))
#:> 2000-01-01
#:> 2000-01-01 00:00:00
#:> 2000-01-01 23:15:55
#:> <class 'datetime.date'>
#:> <class 'datetime.datetime'>
9.5.2 Class Method
9.5.2.1 now
and today
Both now()
and today()
return current system local datetime, no timezone
print( dt.datetime.now(), '\n',
dt.datetime.now().date())
#:> 2020-11-20 14:28:25.898918
#:> 2020-11-20
dt.datetime.today()
#:> datetime.datetime(2020, 11, 20, 14, 28, 25, 906038)
9.5.2.3 combine()
date and time
Apply datetime.combine()
module method on both date and time object to get datetime
= dt.datetime.now()
now dt.datetime.combine(now.date(), now.time())
#:> datetime.datetime(2020, 11, 20, 14, 28, 25, 922861)
9.5.2.4 Convert from String strptime()
Use strptime
to convert string into datetime object
%I : 12-hour
%H : 24-hour
%M : Minute
%p : AM/PM
%y : 18
%Y : 2018
%b : Mar
%m : month (1 to 12)
%d : day
'2011-02-25','%Y-%m-%d') datetime.strptime(
#:> datetime.datetime(2011, 2, 25, 0, 0)
'9-01-18','%d-%m-%y') datetime.strptime(
#:> datetime.datetime(2018, 1, 9, 0, 0)
'09-Mar-2018','%d-%b-%Y') datetime.strptime(
#:> datetime.datetime(2018, 3, 9, 0, 0)
'2/5/2018 4:49 PM', '%m/%d/%Y %I:%M %p') datetime.strptime(
#:> datetime.datetime(2018, 2, 5, 16, 49)
9.5.2.5 Convert from ISO fromisoformat
-
fromisoformat()
is intend to be reverse ofisoformat()
- It actually not ISO compliance: when Z or +8 is included at the end of the string, error occur
#s = dt.datetime.now().isoformat()
"2019-02-05T10:22:33") dt.datetime.fromisoformat(
#:> datetime.datetime(2019, 2, 5, 10, 22, 33)
9.5.3 Instance Method
9.5.3.2 replace
=1999) datetime.now().replace(year
#:> datetime.datetime(1999, 11, 20, 14, 28, 25, 988527)
9.5.3.5 Convert to String
str
str( datetime.now() )
#:> '2020-11-20 14:28:26.024638'
Use strftime()
'%d-%b-%Y') dt.datetime.now().strftime(
#:> '20-Nov-2020'
'%Y-%m-%dT%H:%M:%S.%fZ') ## ISO 8601 UTC dt.datetime.utcnow().strftime(
#:> '2020-11-20T20:28:26.048793Z'
Use isoformat()
dt.datetime.utcnow().isoformat()
#:> '2020-11-20T20:28:26.056107'
9.6 time
9.6.1 Constructor
print( time(2) ) #default single arugement, hour
#:> 02:00:00
print( time(2,15) ) #default two arguments, hour, minute
#:> 02:15:00
print( time(hour=2,minute=15,second=30) )
#:> 02:15:30
9.7 timedelta
-
years argument is not supported
- Apply timedelta on datetime object
- timedelta cannot be applied on time object , because timedelta potentially go beyond single day (24H)
= timedelta(days=365,minutes=33,seconds=15) delt
= datetime.now()
now print ('delt+now : ', now+delt)
#:> delt+now : 2021-11-20 15:01:41.137995