Python Date & Time

Python provides a built-in module called datetime that allows you to work with dates and times.


Python Date Time Example

  • Importing the datetime module

from datetime import datetime, date, time


  • Getting the current date and time

from datetime import datetime

current_datetime = datetime.now()
print("Current date time is : ", current_datetime)

Output

Current date time is :  2024-01-28 17:26:39.320759


  • Creating a specific date and time

from datetime import datetime, date, time

# Creating a specific date >> Year, Month, Day
my_date = date(2024, 2, 28)
print("Specific date:", my_date)

# Creating a specific time >> Hour, Minite, Second
my_time = time(22, 30, 12)
print("Specific time:", my_time)

# Creating a specific date and time
my_datetime = datetime(2024, 1, 28, 12, 30, 12)
print("Specific date and time:", my_datetime)

Output

Specific date: 2024-02-28
Specific time: 22:30:12
Specific date and time: 2024-01-28 12:30:12


Python Formatting a datetime object as a string using strftime()

Example Code

from datetime import datetime

# Formatting a datetime object as a string
current_time = datetime.now()
formatted_date = current_time.strftime("%d/%m/%Y %H:%M:%S")
print("Formatted date:", formatted_date)

Output

Formatted date: 28/01/2024 17:37:24


Python Parsing a string to datetime object using strptime()

Example Code

from datetime import datetime

# Parsing a string into a datetime object
str_date = "28/01/2024 17:37:24"
parsed_datetime = datetime.strptime(str_date, "%d/%m/%Y %H:%M:%S")
print("Parsed datetime:", parsed_datetime)

Output

Parsed datetime: 2024-01-28 17:37:24


Working with Python timedeltas

In Python, the timedelta class from the datetime module represents the difference between two dates or times. It allows you to perform arithmetic operations with dates and times.

Example

from datetime import timedelta, datetime

current_datetime = datetime.now()

# Creating a timedelta of 1 day
one_day = timedelta(days=1)
print("Tiem delta of one day:", one_day)

# Adding a timedelta to a date
new_date = current_datetime + one_day
print("New date after adding one day:", new_date)

# Subtracting a timedelta from a datetime
new_datetime = current_datetime - one_day
print("New datetime after subtracting one day:", new_datetime)

Output

Tiem delta of one day: 1 day, 0:00:00
New date after adding one day: 2024-01-29 17:45:26.171934
New datetime after subtracting one day: 2024-01-27 17:45:26.171934


Python timedeltas with all params

from datetime import timedelta

# Creating a timedelta of 1 day
one_day = timedelta(days=1)
print("Tiem delta of one day:", one_day)

# Creating a timedelta of 2 hours
two_hours = timedelta(hours=2)
print("Tiem delta of two hours:", two_hours)

# Creating a custom timedelta with days, hours, minutes, and seconds
custom_timedelta = timedelta(days=3, hours=5, minutes=30, seconds=45)
print("Tiem delta of all fields:", custom_timedelta)

Output

Tiem delta of one day: 1 day, 0:00:00
Tiem delta of two hours: 2:00:00
Tiem delta of all fields: 3 days, 5:30:45


Python date and time formatting symbols

Name Directive Output Description

Day

%d

12

Day of month 01-31

Month

%b

Dec

Month name, short version

%B

December

Month name, full version

%m

12

Month as a number 01-12

Year

%y

22

Short version, without century

%Y

2022

Full version

%j

360

Day number of year 001-366

Hour

%H

13

Hour 00-23

%I

12

Hour 00-12

%p

AM

AM/PM

Minute

%M

12

Minute 00-59

Second

%S

12

Second 00-59

Microsecond

%f

121212

Microsecond 000000-999999

Weekday

%a

Wed

Short version

%A

Wednesday

Full version

%w

1

Weekday as a number 0-6, 0 is Sunday

%U

48

Week number of year, Sunday as the first day of week, 00-53

%W

50

Week number of year, Monday as the first day of week, 00-53

Time Zone

%z

+0100

UTC offset

%Z

CST

Time zone name

Century

%C

20

Century

Local

%c

Mon Dec 12 12:12:00 2021

Local version of date and time

%x

12/31/21

Local version of date

%X

12:12:00

Local version of time