Tutorial Gateway

  • C
  • C#
  • Java
  • Python
  • SQL
  • MySQL
  • Js
  • BI Tools
    • Informatica
    • Talend
    • Tableau
    • Power BI
    • SSIS
    • SSRS
    • SSAS
    • MDX
    • R Tutorial
    • Alteryx
    • QlikView
  • More
    • C Programs
    • C++ Programs
    • Go Programs
    • Python Programs
    • Java Programs

Python strftime

by suresh

The Python strftime function is one of the datetime module and time module function. This Python strftime function helps you to format the given dates into the required string format. The syntax of the Python strftime function in the datetime module isĀ 

datetime_Object_Value.strftime(Directives)

The syntax of the Python strftime function in the time module is 

time.strftime(Directives, time_object_value)

The List of available directives and their descriptions in Python strftime function.

DirectivesDescriptionExample Output
%aShort Version of WeekdayWed
%AFull Version of WeekdayWednesday
%bShort Version of Month NameJan
%BFull Version of Month NameJanuary
%cLocal Version of Date and TimeThu May 23 10:35:32 2019
%dDay Number of a Month 01 to 3123
%fMicroseconds 000000 to 999999965060
%HHours 00 to 2315
%IHours 00 to 1210
%jDay Number of the Year 001 to 366320
%mMonth as a Number 01 to 1205
%MMinutes from 00 to 5935
%pAM or PMAM
%SSeconds from 00 to 5932
%UWeek Number of the Year from 00 to 53, where First Day of the Week = Sunday 20
%wWeekday as a Number from 0 to 6, Where 0 = Sunday4
%WWeek Number of the Year from 00 to 53, where First Day of the Week = Monday20
%xLocal Version of Date05/23/19
%XLocal Version of Time10:35:32
%yShort Version of Year19
%YFull Version of the Year2019
%zUTC Offset 
%ZTimezone 
%%A % character%

Python strftime Examples

We use the strftime function to return the local version of Date, Time, and DateTime of the current date and time.

# Python strftime Example
from datetime import datetime
 
dt = datetime.now()
 
print('Current Date and Time          = ', dt)
 
print('Local Version of Date and Time = ', dt.strftime('%c'))
print('Local Version of Date          = ', dt.strftime('%x'))
print('Local Version of Time          = ', dt.strftime('%X'))
Python strftime 1

Using the strftime to return the Year, Month name, and Day Number from the current date.

from datetime import datetime
 
dt = datetime.now()
 
print('Current Date and Time = ', dt)
 
print('Current Year          = ', dt.strftime('%Y'))
print('Month Name            = ', dt.strftime('%B'))
print('Day Number            = ', dt.strftime('%d')) 
Python strftime 2

This Python example returns Year, Month name, Month Number, Day Number, etc., from today’s date and time.

from datetime import datetime
 
dt = datetime.now()
 
print('Current Date and Time    = ', dt)
print('Year in Short            = ', dt.strftime('%y'))
print('Year                     = ', dt.strftime('%Y'))
print('Month Name in Short      = ', dt.strftime('%b'))
print('Month Name               = ', dt.strftime('%B'))
print('Month Number             = ', dt.strftime('%m'))
print('Day Number of the Month  = ', dt.strftime('%d'))
print('Day Number of the Year   = ', dt.strftime('%j'))
print('Week Number of Year      = ', dt.strftime('%W'))
print('Short Version of Weekday = ', dt.strftime('%a'))
print('Full Version of Weekday  = ', dt.strftime('%A'))
print('Weekday as a Number      = ', dt.strftime('%w'))
Python strftime 3

Let me use this Python strftime to return the Hours, Minutes, Seconds, Microseconds, AM or PM from current DateTime.

from datetime import datetime
 
dt = datetime.now()
 
print('Current Date and Time = ', dt)
print('Hours                 = ', dt.strftime('%H'))
print('Hours                 = ', dt.strftime('%I'))
print('Minutes               = ', dt.strftime('%M'))
print('Seconds               = ', dt.strftime('%S'))
print('Microseconds          = ', dt.strftime('%f'))
print('AM or PM              = ', dt.strftime('%p'))
Python strftime 4

W are using all the available directives in one example. It might helps you to see all of them in one place.

from datetime import datetime
 
dt = datetime.now()
 
print('Today\'s Date and Time             = ', dt)
 
print('Local Version of Date and Time     = ', dt.strftime('%c'))
print('Local Version of Date              = ', dt.strftime('%x'))
print('Local Version of Time              = ', dt.strftime('%X'))
 
print('Short Version of the Year          = ', dt.strftime('%y'))
print('Full Version of the Year           = ', dt.strftime('%Y'))
 
print('Short Version of Month Name        = ', dt.strftime('%b'))
print('Full Version of Month Name         = ', dt.strftime('%B'))
print('Month Number from 01 to 12         = ', dt.strftime('%m'))
 
print('Day Number of the Month from 01-31 = ', dt.strftime('%d'))
print('Day Number of Year from 001 to 366 = ', dt.strftime('%j'))
 
print('Week Number of Year from 00 to 53  = ', dt.strftime('%U'))
print('Week Number of Year from 00 to 53  = ', dt.strftime('%W'))
 
print('Short Version of Weekday           = ', dt.strftime('%a'))
print('Full Version of Weekday            = ', dt.strftime('%A'))
print('Weekday as a Number from 0 to 6    = ', dt.strftime('%w'))
 
print('Hours from 00 to 23 (24hr Format)  = ', dt.strftime('%H'))
print('Hours from 00 to 12 (12hr Format)  = ', dt.strftime('%I'))
print('Minutes from 00 to 59              = ', dt.strftime('%M'))
print('Seconds from 00 to 59              = ', dt.strftime('%S'))
print('Microseconds from 000000 to 999999 = ', dt.strftime('%f'))
print('AM or PM                           = ', dt.strftime('%p'))
print('% Charcter                         = ', dt.strftime('%%'))
Python strftime 5

Python strftime to format DateTime

So far, we are using one directive to return one part of the Date. However, Python strftime is very useful to format the date and time in the required format. It displays the current date and time in Year/month/day Hour:Minute: Second AM/PM.

import datetime
 
dt = datetime.datetime.now()
 
print('Current Date and Time  = ', dt)
 
print('Current Date and Time  = ', dt.strftime('%Y/%m/%d %H:%M:%S %p'))
Python strftime 6

Few more Python strftime formatting options.

from datetime import datetime
 
dt = datetime.now()
 
print('Current Date and Time    = ', dt)
 
print('Formatted Date and Time  = ', dt.strftime('%Y/%M/%d %H:%M:%S %p'))
print('Formatted Date and Time  = ', dt.strftime('%m/%d/%y %H:%M:%S'))
print('Formatted Date and Time  = ', dt.strftime('%m-%d-%Y %H:%M:%S'))
print('Formatted Date and Time  = ', dt.strftime('%d-%m-%Y %I:%M:%S %p'))
 
print('Formatted Date and Time  = ', dt.strftime('%Y, %B %d'))
print('Formatted Date and Time  = ', dt.strftime('%Y, %b %d'))
 
print('Formatted Date and Time  = ', dt.strftime('%B %d %y'))
print('Formatted Date and Time  = ', dt.strftime('%B %d, %Y %I-%M-%S %p'))
Python strftime 7

Python strftime in time Module

You can also use the time library to use this strftime. Here, we used the strftime function inside the time library.

import time
 
dt = time.localtime(1234567)
 
print('Formatted Date and Time  = ', time.strftime('%Y/%M/%d %H:%M:%S %p', dt))
print('Formatted Date and Time  = ', time.strftime('%m/%d/%y %H:%M:%S', dt))
print('Formatted Date and Time  = ', time.strftime('%d-%m-%Y %I:%M:%S %p', dt))
 
print('Formatted Date and Time  = ', time.strftime('%I:%M:%S %p', dt))
print('Formatted Date and Time  = ', time.strftime('%I-%M-%S %p', dt))
Python strftime 8

Placed Under: Python

  • Download and Install Python
  • Python Arithmetic Operators
  • Python Assignment Operators
  • Python Bitwise Operators
  • Python Comparison Operators
  • Python Logical Operators
  • Python If Statement
  • Python If Else
  • Python Elif Statement
  • Python Nested If
  • Python For Loop
  • Python While Loop
  • Python Break
  • Python Continue
  • Python Dictionary
  • Python datetime
  • Python String
  • Python Set
  • Python Tuple
  • Python List
  • Python List Comprehensions
  • Python Lambda Function
  • Python Functions
  • Python Types of Functions
  • Python Iterator
  • Python File Handling
  • Python Directory
  • Python Class
  • Python classmethod
  • Python Inheritance
  • Python Method Overriding
  • Python Static Method
  • Connect Python and SQL Server
  • Python SQL Create DB
  • Python SQL Select Top
  • Python SQL Where Clause
  • Python SQL Order By
  • Python SQL Select Statement
  • Python len Function
  • Python max Function
  • Python map Function
  • Python print Function
  • Python sort Function
  • Python range Function
  • Python zip Function
  • Python Math Functions
  • Python String Functions
  • Python List Functions
  • Python NumPy Array
  • NumPy Aggregate Functions
  • NumPy Arithmetic Operations
  • Python Numpy Bitwise operators
  • Numpy Comparison Operators
  • Numpy Exponential Functions
  • Python Numpy logical operators
  • Python numpy String Functions
  • NumPy Trigonometric Functions
  • Python random Array
  • Python numpy concatenate
  • Python numpy Array shape
  • Python pandas DataFrame
  • Pandas DataFrame plot
  • Python Series
  • Python matplotlib Histogram
  • Python matplotlib Scatter Plot
  • Python matplotlib Pie Chart
  • Python matplotlib Bar Chart
  • Python List Length
  • Python sort List Function
  • Python String Concatenation
  • Python String Length
  • Python substring
  • Python Programming Examples

Copyright © 2021 · All Rights Reserved by Suresh

About Us | Contact Us | Privacy Policy