Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
How to 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
The strftime() Function
The strftime() function returns a formatted date and time string. It accepts a format string that you can use to get the result you want. The following are the most commonly used format directives ?
| Directive | Meaning | Example |
|---|---|---|
| %Y | Year with century | 2025 |
| %y | Year without century | 25 |
| %m | Month as number [01-12] | 05 |
| %B | Full month name | January |
| %b | Abbreviated month name | Jan |
| %d | Day of month [01-31] | 09 |
| %H | Hour (24-hour) [00-23] | 14 |
| %I | Hour (12-hour) [01-12] | 02 |
| %M | Minute [00-59] | 30 |
| %S | Second [00-61] | 45 |
| %p | AM or PM | AM |
Example
In the example below, we are using the strftime() function to get the formatted date and time in various formats ?
from datetime import datetime
# getting the current date and time
current_datetime = datetime.now()
# getting individual components
current_year = current_datetime.strftime("%Y")
print("Current year:", current_year)
current_month = current_datetime.strftime("%m")
print("Current month:", current_month)
current_day = current_datetime.strftime("%d")
print("Current day:", current_day)
# getting formatted time
current_time = current_datetime.strftime("%H:%M:%S")
print("Current time:", current_time)
# getting complete date and time in custom format
current_date_time = current_datetime.strftime("%m/%d/%Y, %H:%M:%S")
print("Current date and time:", current_date_time)
Current year: 2025 Current month: 01 Current day: 15 Current time: 14:30:45 Current date and time: 01/15/2025, 14:30:45
Working with Time Class
The time class represents time values with hour, minute, second, and microsecond components ?
from datetime import time
# create a time object
format_time = time(9, 30, 45, 123456)
print("Time object:", format_time)
# access individual attributes
print(f"Hour: {format_time.hour}")
print(f"Minute: {format_time.minute}")
print(f"Second: {format_time.second}")
print(f"Microsecond: {format_time.microsecond}")
Time object: 09:30:45.123456 Hour: 9 Minute: 30 Second: 45 Microsecond: 123456
Working with Date Class
The date class represents calendar date values with year, month, and day attributes ?
from datetime import date
# create a date object
format_date = date(2025, 4, 16)
print("Date object:", format_date)
# access individual attributes
print(f"Year: {format_date.year}")
print(f"Month: {format_date.month}")
print(f"Day: {format_date.day}")
Date object: 2025-04-16 Year: 2025 Month: 4 Day: 16
Converting Date to String
You can convert datetime objects to strings using strftime() with custom format codes ?
from datetime import datetime
# create datetime object
dt = datetime(2025, 4, 16, 15, 30, 45)
# different string formats
print("Default format:", dt)
print("Custom format 1:", dt.strftime("%B %d, %Y at %I:%M %p"))
print("Custom format 2:", dt.strftime("%d-%m-%Y %H:%M:%S"))
print("Date only:", dt.strftime("%Y-%m-%d"))
print("Time only:", dt.strftime("%H:%M:%S"))
Default format: 2025-04-16 15:30:45 Custom format 1: April 16, 2025 at 03:30 PM Custom format 2: 16-04-2025 15:30:45 Date only: 2025-04-16 Time only: 15:30:45
Converting String to Date
Use strptime() to convert date strings back to datetime objects. The format string must match the input string exactly ?
from datetime import datetime
# convert various string formats to datetime
date_str1 = "16/04/2025"
dt1 = datetime.strptime(date_str1, "%d/%m/%Y")
print("From DD/MM/YYYY:", dt1)
date_str2 = "April 16, 2025"
dt2 = datetime.strptime(date_str2, "%B %d, %Y")
print("From Month DD, YYYY:", dt2)
date_str3 = "2025-04-16 15:30:45"
dt3 = datetime.strptime(date_str3, "%Y-%m-%d %H:%M:%S")
print("From YYYY-MM-DD HH:MM:SS:", dt3)
From DD/MM/YYYY: 2025-04-16 00:00:00 From Month DD, YYYY: 2025-04-16 00:00:00 From YYYY-MM-DD HH:MM:SS: 2025-04-16 15:30:45
Conclusion
Python's datetime module provides powerful tools for formatting dates and times. Use strftime() to convert datetime objects to formatted strings, and strptime() to parse strings back to datetime objects with matching format codes.
