The datetime
class in the datetime
module provide a structured approach in managing date and time data. It provides a wide range of tools and methods that enable easy manipulation of dates and time values.
from datetime import datetime
print(datetime.now())
In the above example, we used the now()
class method to get the current date and time values.
from datetime import datetime
pi_day = datetime(year = 2023, month = 3, day = 14, hour = 11, minute = 30, second = 30, microsecond = 500)
print(pi_day)
Timedelta objects, on the other hand, are used to represent a duration in terms of days, seconds, and microseconds.
from datetime import timedelta
td = timedelta(days=5, seconds=120, microseconds=50000, milliseconds=3000, minutes=10, hours=3, weeks=1)
print(td)
A timedelta
can be defined as a difference between two dates or datetime
objects.
Performing arithmetic operations on datetimes
We can use the standard operators on datetime
objects. For example we can add two time to a objects together, subtract one from the other, compare two of them using comparison operators such as '<', '>', and '==', and access their individual components such as year, month, and day using dot notation.
Subtraction
from datetime import datetime
today = datetime.now()
past = datetime(2023, 3, 18)
#This returns a timedelta object
print(today - past)
Whenever we subtract two datetime
objects, as in above, a timedelta
is returned representing the time between the two days. We can also similarly subtract a timedelta
from a datetime
to get an earlier datetime
.
from datetime import datetime, timedelta
today = datetime.now()
one_day = timedelta(days = 1)
yesterday = today - one_day
print('today:', today)
print('yesterday: ', yesterday)
In the above example we instantiated a timedelta
object which holds a duration of one day. On subtracting now with the timdelta
we get yesterday's datetime
object.
from datetime import datetime, timedelta
today = datetime.now()
one_week = timedelta(weeks = 1)
last_week = today - one_week
print(last_week)
Addition
When we add two datetime
objects, the result will be a timedelta
object representing the duration of time between the two datetime
objects. We can even more usefully add a timedelta
object to a datetime
object, and the result will be a new datetime
object that is the original datetime
plus the timedelta
duration.
from datetime import datetime, timedelta
today = datetime.now()
one_day = timedelta(days = 1)
tomorrow = today + one_day
print('today: ', today.ctime(), '\n')
print('tomorrow: ', tomorrow.ctime())
from datetime import datetime, timedelta
today = datetime.now()
one_week = timedelta(weeks = 1)
next_week = today + one_week
print('today: ', today.ctime(), '\n')
print('next week: ', next_week.ctime())
Comparing datetimes
We can use comparison operators to compare datetime
objects in order to get the relation between them.
from datetime import datetime, timedelta
one_day = timedelta(days = 1)
two_days = timedelta(days = 2)
today = datetime.now()
yesterday = today - one_day
tomorrow = today + one_day
#use the operators
print(today > tomorrow)
print(today > yesterday)
print(yesterday == (tomorrow - two_days) )