Get Formatted Date and Time in Python



This article will discuss how to format date and time in Python. Python makes it easy to work with date and time with the help of the datetime module. You can format dates and times in different formats as per your needs.

Datetime Module of Python

The datetime module in Python offers methods for working with date and time values. To use this module, we must first import it using the following import keyword-

import datetime

Strftime() Function

The strftime() function returns a formatted date and time. It accepts a format string that you can use to get the result you want. The following are the directives that it supports.

Directive Meaning
%a Locale's abbreviated weekday name
%B Locale's full month name.
%c Locale's appropriate date and time representation
%d Day of the month as a decimal number [01,31].
%H Hour (24-hour clock) as a decimal number [00,23].
%I Hour (12-hour clock) as a decimal number [01,12].
%m Month as a decimal number [01,12].
%M Minute as a decimal number [00,59].
%p Locale's equivalent of either AM or PM.
%S Second as a decimal number [00,61].
%U Week number of the year (Sunday as the first day of the week) as a decimal number [00,53]. All days in a new year preceding the first Sunday are considered to be in week 0.
%w Weekday as a decimal number [0(Sunday),6].
%W Week number of the year (Monday as the first day of the week) as a decimal number [00,53]. All days in a new year preceding the first Monday are considered to be in week 0.
%x Locale's appropriate date representation.
%X Locale's appropriate time representation.
%y Year without century as a decimal number [00,99].
%Y Year with century as a decimal number.
%Z Time zone name (no characters if no time zone exists).
%% A literal "%" character

Using a date, time, or datetime object, the strftime() method returns a string representing the date and time. To convert a datetime object into a string using the specified format, use datetime.strftime(format).

The format codes are standard directives for specifying the format in which you want to represent a datetime. The %d-%m-%Y%H:%M:%S codes, for example, convert dates to dd-mm-yyyy hh:mm:ss format.

Example

In the example below, we are using the strftime() function to get the formatted date and time.

from datetime import datetime

# getting the current date and time
current_datetime = datetime.now()

# getting the year from the current date and time
current_year = current_datetime.strftime("%Y")
print("current year = ", current_year)

# getting the month from the current date and time
current_month = current_datetime.strftime("%m")
print("current month = ", current_month)

# getting the day/date from the current date and time
current_day = current_datetime.strftime("%d")
print("current day = ", current_day)

# getting the time from the current date and time in the given format
current_time = current_datetime.strftime("%H:%M:%S")
print("current time = ", current_time)

# getting the date and time from the current date and time in the given format
current_date_time = current_datetime.strftime("%m/%d/%Y, %H:%M:%S")
print("current date and time = ",current_date_time)

Output

Running the program will produce the following output:

current year =  2025
current month =  05
current day =  09
current time =  12:57:38
current date and time =  05/09/2025, 12:57:38

Note: If the program is running on an online editor, it shows GMT time, whereas if the program is running on Local System Editor, it will show the time displayed on your own.

Time class

The time class can be used to represent time values. The hour, minute, second, and microsecond are time class attributes.

Syntax

time(hour, minute, second, microsecond)

Example 1

Below is an example to show how you can use the time class -

import datetime

# format time
format_time = datetime.time(3, 5, 25, 10)
print(format_time)

Output

The program gives this output when it is run:

03:05:25.000010

There are time attribute ranges, for example, seconds have a range of 0 to 59 and nanoseconds have a range of 0 to 999999. If the range is too large, the compiler displays a ValueError

The instance of the time class has three instance attributes: hour, minute, second, and microsecond. These are used to obtain specific time information

Example 2

Here is an example to show how you can use the time class -

import datetime

# format time
format_time = datetime.time(9, 5, 25, 10)

# Printing time in the format of time attributes
print('Time:',
   format_time.hour,' hours ',
   format_time.minute,' minutes ',
   format_time.second,'seconds and ',
   format_time.microsecond,'microseconds' )

Output

You will see this result after executing the program:

Time: 9  hours  5  minutes  25 seconds and  10 microseconds

Date class

The calendar date values can be represented using the date class. The date instance is made up of the year, month, and day attributes.

Syntax

date(yyyy, mm, dd)

Example 1

Here is an example to show how you can use the date class -

import datetime
# format date
format_date = datetime.date(2005, 4, 16)

# Printing date in the format of date attributes
print('Date:', format_date.day,
   'day,', format_date.month,
   'month and', format_date.year, 'year')

Output

This output will be displayed when the program runs:

Date: 16 day, 4 month and 2005 year

Convert date to string

Because dates and times differ from strings, it is frequently necessary to convert the datetime to a string. We use the strftime() function for this.

Example

In the example below, we are converting a datetime object to a string with the help of the strftime() function.

import datetime

# format date and time
format_datetime = datetime.datetime(2005, 4, 16, 3, 22, 30, 8)

# Printing date & time in the format of date and time attributes
print(format_datetime.strftime("%b %d %Y %H:%M:%S"))

Output

After running the program, you will get this result:

Apr 16 2005 03:22:30

The symbols %H,%M, and %S represent the hour, minutes, and seconds, respectively. The characters %b, %d, and %Y represent the month, day, and year, respectively.

Convert String to Date

Conversion from string to date is frequently required when working with imported data sets from a csv or taking inputs from website forms. Python has a function called strptime() to accomplish this.

Example

In the example below, we are converting a string to a date using the strptime() function.

from datetime import datetime

# converting input string to date format(date-month-year format)
print(datetime.strptime('16/5/2025', '%d/%m/%Y'))

Output

When you run the program, it will show this output:

2025-05-16 00:00:00
Updated on: 2025-05-16T17:49:47+05:30

111K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements