Skip to content
Related Articles

Related Articles

Improve Article
Python strftime() function
  • Last Updated : 15 Mar, 2021

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

Syntax :

strftime(format)

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

List of format codes : Reference table for the format codes.

DirectiveMeaningOutput Format
%aAbbreviated weekday name.Sun, Mon, …
%AFull weekday name.Sunday, Monday, …
%wWeekday as a decimal number.0, 1, …, 6
%dDay of the month as a zero added decimal.01, 02, …, 31
%-dDay of the month as a decimal number.1, 2, …, 30
%bAbbreviated month name.Jan, Feb, …, Dec
%BFull month name.January, February, …
%mMonth as a zero added decimal number.01, 02, …, 12
%-mMonth as a decimal number.1, 2, …, 12
%yYear without century as a zero added decimal number.00, 01, …, 99
%-yYear without century as a decimal number.0, 1, …, 99
%YYear with century as a decimal number.2013, 2019 etc.
%HHour (24-hour clock) as a zero added decimal number.00, 01, …, 23
%-HHour (24-hour clock) as a decimal number.0, 1, …, 23
%IHour (12-hour clock) as a zero added decimal number.01, 02, …, 12
%-IHour (12-hour clock) as a decimal number.1, 2, … 12
%pLocale’s AM or PM.AM, PM
%MMinute as a zero added decimal number.00, 01, …, 59
%-MMinute as a decimal number.0, 1, …, 59
%SSecond as a zero added decimal number.00, 01, …, 59
%-SSecond as a decimal number.0, 1, …, 59
%fMicrosecond as a decimal number, zero added on the left.000000 – 999999
%zUTC offset in the form +HHMM or -HHMM. 
%ZTime zone name. 
%jDay of the year as a zero added decimal number.001, 002, …, 366
%-jDay of the year as a decimal number.1, 2, …, 366
%UWeek 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
%WWeek 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

Example:






# 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

 Attention geek! Strengthen your foundations with the Python Programming Foundation Course and learn the basics.  

To begin with, your interview preparations Enhance your Data Structures concepts with the Python DS Course. And to begin with your Machine Learning Journey, join the Machine Learning – Basic Level Course




My Personal Notes arrow_drop_up
Recommended Articles
Page :