What is Date & Time Functions in Python?

Updated: May 18



Date, time and datetime modules give a number of functions to contract with dates, times and time intervals in Python. We manipulate dates or time then need to import datetime function. In Python date and datetime are an object, therefore when we manipulate them, we are actually manipulating objects and not string or timestamps.

There are 5 main classes of datetime function in Python which are below:

date – Manipulate just date ( Month, day, year)

time – Time free of the day (Hour, minute, second, microsecond)

datetime – Grouping of time and date (Month, day, year, hour, second, microsecond)

timedelta— A duration of time used for manipulating dates

tzinfo— An abstract class for dealing with time zones

Get Current Date and Time

Example:

import datetime

datetime_object = datetime.datetime.now()

print(datetime_object)

Output:

2020-04-03 04:38:50.752712

We import datetime module using import datetime statement and is used now() method to create a datetime object containing the current local date and time.

Get Current Date

import datetime

date_object = datetime.date.today()

print(date_object)

Output:

2020-04-03

We have used today () is used to defined the current local date.

datetime.time

A time object instantiated from the time class represents the local time.



Print hour, minute, second and microsecond

By creating a time object, we can simply print its attributes such as hour, minute etc.

from datetime import time

a = time(11, 34, 56)

print("hour =", a.hour)

print("minute =", a.minute)

print("second =", a.second)

print("microsecond =", a.microsecond)

Output :

hour = 11

minute = 34

second = 56

microsecond = 0

datetime.datetime

The datetime module has a class named dateclass that can contain information from both date and time objects.

Python datetime object

Example:

from datetime import datetime

a = datetime(2020, 10, 25)

print(a)

b = datetime(2019, 10, 25, 23, 54, 46, 642380)

print(b)

Output:

2020-10-25 00:00:00

2019-10-25 25:23:46.642380

datetime.timedelta

A timedelta object represents the difference between two dates or times.

Example:

from datetime import datetime, date

t1 = date(year = 2018, month = 7, day = 12)

t2 = date(year = 2017, month = 12, day = 23)

t3 = t1 - t2

print("t3 =", t3)

t4 = datetime(year = 2018, month = 7, day = 12, hour = 7, minute = 9, second = 33)

t5 = datetime(year = 2019, month = 6, day = 10, hour = 5, minute = 55, second = 13)

t6 = t4 - t5

print("t6 =", t6)

print("type of t3 =", type(t3))

print("type of t6 =", type(t6))

Output:

t3 = 201 days, 0:00:00

t6 = -333 days, 1:14:20

type of t3 = <class 'datetime.timedelta'>

type of t6 = <class 'datetime.timedelta'>


Handling timezone in Python: We use a third-party pytZ module to display date and time based on their timezone.

Example:

from datetime import datetime

import pytz

local = datetime.now()

print("Local:", local.strftime("%m/%d/%Y, %H:%M:%S"))

tz_NY = pytz.timezone('America/New_York')

datetime_NY = datetime.now(tz_NY)

print("NY:", datetime_NY.strftime("%m/%d/%Y, %H:%M:%S"))

tz_London = pytz.timezone('Europe/London')

datetime_London = datetime.now(tz_London)

print("London:", datetime_London.strftime("%m/%d/%Y, %H:%M:%S"))

Output:

Local time: 2018-12-20 13:10:44.260462

America/New_York time: 2018-12-20 13:10:44.260462

Europe/London time: 2018-12-20 13:10:44.260462