The Wayback Machine - https://web.archive.org/web/20240615032156/https://www.geeksforgeeks.org/python-strftime-function/
Open In App

Python strftime() function

Last Updated : 24 Jul, 2023
Improve
Improve
Like Article
Like
Save
Share
Report

The Strftime() function is used to convert date and time objects to their string representation. It takes one or more inputs of formatted code and returns the string representation in Python.

Python Strftime() Syntax

Syntax: datetime_obj.strftime(format)

Parameters: 

  • Format: consists of various format codes that define specific parts of the date and time.

Returns: It returns the string representation of the date or time object.

Strftime() Method in Python Example

The Python datetime library includes a method called strftime(). You can utilize a variety of the format codes it offers to build the necessary string representation. Let’s explore the Python date time string strftime() function’s example:

Python3




from datetime import datetime
now = datetime.now()
formatted = now.strftime("%Y-%m-%d %H:%M:%S")
print(formatted)


Output

2023-07-18 06:24:12

Time complexity: O(1)
Auxiliary Space: O(1)

Using Strftime() from Datetime Module

In this code, we are using the strftime() method from the datetime module to convert python date time string.

Python3




# Python program to demonstrate
# strftime() function
 
from datetime import datetime as dt
 
# Getting current date and time
now = dt.now()
print("Without formatting", now)
 
# Example 1
s = now.strftime("%a %m %y")
print('\nExample 1:', s)
 
# Example 2
s = now.strftime("%A %m %Y")
print('\nExample 2:', s)
 
# Example 3
s = now.strftime("%I %p %S")
print('\nExample 3:', s)
 
# Example 4
s = now.strftime("%j")
print('\nExample 4:', s)


Output

Without formatting 2019-12-17 18:21:39.211378
Example 1: Tue-12-19
Example 2: Tuesday-12-2019
Example 3: 6 PM 39
Example 4: 351

Time complexity: O(1)
Auxiliary Space: O(1)

Formatting Date and Time with AM/PM

In this code, we are using the strftime() method from the datetime module to format the date and time with AM/PM format to Python date time string.

Python3




from datetime import datetime as dt
 
date = dt.now()
formatted_date = date.strftime("%B %d, %Y")
print(formatted_date)
 
time = dt.now()
formatted_time = time.strftime("%I:%M:%S %p")
print(formatted_time)


Output

July 18, 2023
06:52:17 AM

Time complexity: O(1)
Auxiliary Space: O(1)

Combining Different Format Codes

In this code, we are using the strftime() method from the datetime module combining different format codes for a specific format and also custom string with multiple format codes to Python date time string.

Python3




from datetime import datetime as dt
 
custom = dt.now()
formatted_custom = custom.strftime("Today is %A, %B %d, %Y")
print(formatted_custom)
 
combined = dt.now()
formatted_combined = combined.strftime("%d/%m/%Y %H:%M:%S")
print(formatted_combined)


Output

Today is Tuesday, July 18, 2023
18/07/2023 06:50:49

Time complexity: O(1)
Auxiliary Space: O(1)

List of Format Codes

Reference table for the format codes.

Directive Meaning Output Format
%a Abbreviated weekday name. Sun, Mon,….
%A Full weekday name. Sunday, Monday,…..
%w Weekday as a decimal number. 0, 1,….., 6
%d Day of the month as a zero added decimal. 01, 02,…., 31
%-d Day of the month as a decimal number. 1, 2,…., 30
%b Abbreviated month name. Jan, Feb,…., Dec
%B Full month name. January, February,….
%m Month as a zero added decimal number. 01, 02,…., 12
%-m Month as a decimal number. 1, 2,….., 12
%y Year without century as a zero added decimal number. 00, 01,…, 99
%-y Year without century as a decimal number. 0, 1,…, 99
%Y Year with century as a decimal number. 2013, 2019 etc.
%H Hour (24-hour clock) as a zero added decimal number. 00, 01,….., 23
%-H Hour (24-hour clock) as a decimal number. 0, 1,…., 23
%I Hour (12-hour clock) as a zero added decimal number. 01, 02,…, 12
%-I Hour (12-hour clock) as a decimal number. 1, 2,…,12
%p Locale’s AM or PM. AM, PM
%M Minute as a zero added decimal number. 00, 01,…., 59
%-M Minute as a decimal number. 0, 1,…, 59
%S Second as a zero added decimal number. 00, 01,…, 59
%-S Second as a decimal number. 0, 1,…., 59
%f Microsecond as a decimal number, zero added on the left. 000000 – 999999
%z UTC offset in the form +HHMM or -HHMM.  
%Z Time zone name.  
%j Day of the year as a zero added decimal number. 001, 002,….., 366
%-j Day of the year as a decimal number. 1, 2,…., 366
%U Week number of the year (Sunday as the first day of the week). All days in a new year preceding the first Sunday are considered to be in week 0. 00, 01,…., 53
%W Week number of the year (Monday as the first day of the week). All days in a new year preceding the first Monday are considered to be in week 0. 00, 01,….., 53



Similar Reads

time.strftime() function in Python
As time module provides various time-related functions. So it is necessary to import the time module otherwise it will through error because of the definition of time.strftime(format[, t]) is present in time module.time.strftime(format[, t]) function convert a tuple or struct_time representing a time as returned by gmtime() or localtime() to a stri
4 min read
Python | Pandas DatetimeIndex.strftime()
Python is a great language for doing data analysis, primarily because of the fantastic ecosystem of data-centric python packages. Pandas is one of those packages and makes importing and analyzing data much easier. Pandas DatetimeIndex.strftime() function convert to Index using specified date_format. The function return an Index of formatted strings
2 min read
Python | Pandas Period.strftime
Python is a great language for doing data analysis, primarily because of the fantastic ecosystem of data-centric python packages. Pandas is one of those packages and makes importing and analyzing data much easier. Pandas Period.strftime() function returns the string representation of the Period, depending on the selected format. format must be a st
2 min read
Python | Pandas PeriodIndex.strftime
Python is a great language for doing data analysis, primarily because of the fantastic ecosystem of data-centric python packages. Pandas is one of those packages and makes importing and analyzing data much easier. Pandas PeriodIndex.strftime() function return an array of formatted strings specified by date_format, which supports the same string for
2 min read
How to Format date using strftime() in Python ?
In this article, we will see how to format date using strftime() in Python. localtime() and gmtime() returns a tuple representing a time and this tuple is converted into a string as specified by the format argument using python time method strftime(). Syntax: time.strftime(format[, sec]) sec: This is the time in number of seconds to be formatted. f
2 min read
Pandas Series dt.strftime() Method | Change Date Format in Series
The dt.strftime() method converts the datetime objects in the Pandas Series to a specified date format. The function returns an index of formatted strings specified by date_format, which supports the same string format as the Python standard library. Example C/C++ Code import pandas as pd sr = pd.Series(['2012-12-31 08:45', '2019-1-1 12:30', '2008-
2 min read
Python - Call function from another function
Prerequisite: Functions in Python In Python, any written function can be called by another function. Note that this could be the most elegant way of breaking a problem into chunks of small problems. In this article, we will learn how can we call a defined function from another function with the help of multiple examples. What is Calling a Function
4 min read
Wand function() function in Python
function() function is similar to evaluate function. In function() function pixel channels can be manipulated by applies a multi-argument function to pixel channels. Following are the list of FUNCTION_TYPES in Wand: 'undefined''arcsin''arctan''polynomial''sinusoid' Syntax : wand.image.function(function, arguments, channel) Parameters : ParameterInp
1 min read
Returning a function from a function - Python
Functions in Python are first-class objects. First-class objects in a language are handled uniformly throughout. They may be stored in data structures, passed as arguments, or used in control structures. Properties of first-class functions: A function is an instance of the Object type.You can store the function in a variable.You can pass the functi
3 min read
Accessing Python Function Variable Outside the Function
In Python, variables defined within a function have local scope by default. But to Access function variables outside the function usually requires the use of the global keyword, but can we do it without using Global. In this article, we will see how to access a function variable outside the function without using "Global". Accessing Function Variab
3 min read
Article Tags :
Practice Tags :