Table of Contents:
- Chapter 8.3 The
datetime
module
Reference: https://docs.python.org/3/library/datetime.html
The datetime
module supplies different classes for manipulating date and time instances. We can perform different
mathematical operations
The datetime
module exports following constants.
datetime.MINYEAR
: 1datetime.MAXYEAR
: 9999
The datetime
module exports following data types:
The datetime.date
class can create a date
object that accepts 3 parameters, i.e. year
, month
, and day
as
numbers to instantiate the date
object.
initializing date
object
from datetime import date
date_1 = date(1999, 12, 31) # 1999-12-31
date_2 = date(year=1999, month=12, day=31)
The datetime.time
class accepts hour
, minute
, second
, microsecond
, tzinfo
(optional) parameters that has
default values of 0. the datetime.time
class creates time
object.
initializing time
object
from datetime import time
time1 = time(hour=5) # 05:00:00
time2 = time(hour=5, minute=50) # 05:50:00
time3 = time(hour=5, minute=50, second=30) # 05:50:30
time4 = time(hour=5, minute=50, second=30, microsecond=100) # 05:50:30.000100
The class datetime.datetime
is a combination of datetime.date
and datetime.time
classes. It can accept all
attributes of both date
, and time
objects. the parameters of datetime
class are as follows:
datetime(year, month, day[, hour[, minute[, second[, microsecond[,tzinfo]]]]])
The year
, month
and day
arguments are required. tzinfo
may be None, or an instance of a tzinfo subclass. The
remaining
arguments may be ints and has default value 0
.
initializing datetime
object
from datetime import datetime
datetime_1 = datetime(1999, 12, 31, 23, 59, 59, 100) # 1999-12-31 23:59:59.000100
A datetime.timedelta
class is a duration of time that expresses the difference between two date
, time
, or
datetime
instances to microsecond resolution
**initializing the timedelta
object
from datetime import timedelta
d1 = timedelta(hours=10)
d2 = timedelta(days=30, hours=20, minutes=50, seconds=30) # 30 days, 20:50:30
tzinfo
is an abstract base class for time zone information objects. these are used by the datetime and time class to
provide ta customizable notion of time adjustment.
timezone
class is a class that implements tzinfo
abstract base class that has a fixed offset from the UTC.
from datetime import timezone, timedelta
tz1 = timezone(offset=timedelta(hours=5, minutes=45))
We can add timedelta
value to date
or datetime
instance to get the new date
or datetime
.
from datetime import date, datetime, timedelta
date_1 = date(1999, 12, 31)
date_2 = date_1 + timedelta(days=5) # 2000-01-05
datetime_1 = datetime(1999, 12, 31, 12, 30, 50)
datetime_2 = datetime_1 + timedelta(days=5, hours=10) # 2000-01-05 22:30:50
Note: we cannot add two date
or datetime
instances to get a new date
or datetime
instance.
We can subtract one date
or datetime
instance from another date
or datetime
instance to get the time difference
as timedelta
instance.
from datetime import date, datetime
date_1 = date(1999, 12, 31)
date_2 = date(1999, 10, 15)
diff_1 = date_1 - date_2 # 77 days, 0:00:00
datetime_1 = datetime(1999, 12, 31, 12, 30, 50)
datetime_2 = datetime(1999, 10, 15, 19, 45, 21)
diff_2 = datetime_1 - datetime_2 # 76 days, 16:45:29
Note: Subtracting two date
or datetime
instances always give timedelta
instance.
We can parse a datetime string by using datetime.strptime()
method. the strptime()
method takes 2 arguments
date_string
and format
to parse the string.
from datetime import datetime
datetime = datetime.strptime('1999/12/31', '%Y/%m/%d') # 1999-12-31 00:00:00
We can format a date
or datetime
instance to a datetime string by using datetime.strftime()
method.
from datetime import date, datetime
d1 = date(1999, 12, 31)
d_str = d1.strftime('%Y/%m/%d') # 1999/12/31
d_str2 = d1.strftime('%b %d, %Y') # Dec 31, 1999
dt1 = datetime(1999, 12, 31, 23, 59, 30)
dt_str = dt1.strftime('%Y/%m/%d, %HH:%MM:%SS') # 1999/12/31, 23H:59M:30S
%a
: Weekday abbreviated name (Sun
,Mon
, ...,Sat
)%A
: Weekday full name (Sunday
,Monday
, ...,Saturday
)%d
: Day of the month (01
,02
, ...,31
)%b
: Month abbreviated name (Jan
,Feb
, ...,Dec
)%B
: Month full name (January
,February
, ...,December
)%m
: Month as number (01
,02
, ...,12
)%Y
: Year with Century (1999
,2000
, ...)%H
: 24-Hour Clock (00
,01
, ...,23
)%M
: Minutes (00
,01
, ...,59
)%S
: Seconds (00
,01
, ...,59
)
For more formatting options, we can check out https://docs.python.org/3/library/datetime.html#strftime-and-strptime-format-codes.