Strftime method of datetime class in Python

Method Name:

strftime()

 

Method Signature:

strftime(format)

 

Overview:

  • strftime() method will return formatted components of a datetime instance as a string.
  • The return string is constructed based on the C formatting codes given in the format specifier.

 

Example1: Format Codes for getting week and week day information from a datetime instance

 

import datetime

 

# Get today's date with current time

d1 = datetime.datetime.today()

 

# %a will print abbreviated day of the week in three letters

component = d1.strftime("%a")

print(component)

 

# %A will print day of the week as a string

component = d1.strftime("%A")

print(component)

 

# %A will print day of the week as a decimal number - Sunday as 0 and Saturday as 6

component = d1.strftime("%w")

print(component)

 

# %U will print week number of the year

component = d1.strftime("%U")

print(component)

 

Output

 

 

 

Thu

Thursday

4

07

 

Example2: Format codes for getting moth information from a datetime instance

import datetime

 

# Get the date and time for today

d1 = datetime.datetime.today()

 

# Get month from date as a Three Letter Acronym

dateFormatted = d1.strftime("%b")

print(dateFormatted)

 

# Get the whole month from date

dateFormatted = d1.strftime("%B")

print(dateFormatted)

 

# Get the month number as a zero padded decimal number

dateFormatted = d1.strftime("%m")

print(dateFormatted)

 

Output:

Feb

February

02

Example3: Format codes for getting year information from datetime instance

import datetime

 

# Get the date corresponding to the proleptic ordinal 12345

dateObject      = datetime.datetime.fromordinal(12345)

 

#Get the year number as two digits without century information

formattedYear   = dateObject.strftime("%y")

print(formattedYear)

 

#Get the year number as four digits with century information

formattedYear   = dateObject.strftime("%Y")

print(formattedYear)

 

Output:

34

0034

 

Example4: Format codes for getting time components from datetime instance

#import the datetime module of Python

import datetime

 

# Create a datetime instance for UNIX epoch + 50 K seconds

unixEpochDate = datetime.datetime.fromtimestamp(50000)

 

# Print the time components together separated by a pipe symbol

formattedDate = unixEpochDate.strftime("%H Hours|%M Minutes|%S Seconds|%f MicroSeconds")

print(formattedDate)

 

# Get the hour information from datetime - 24 hour clock as zero padded decimal

formattedDate = unixEpochDate.strftime("%H")

print(formattedDate)

 

# Get the hour information from datetime - 12 hour clock as zero padded decimal

formattedDate = unixEpochDate.strftime("%I")

print(formattedDate)

 

# Get the minutes information from datetime

formattedDate = unixEpochDate.strftime("%M")

print(formattedDate)

 

# Get the seconds information from datetime

formattedDate = unixEpochDate.strftime("%S")

print(formattedDate)

 

# Get the microseconds information from datetime

formattedDate = unixEpochDate.strftime("%f")

print(formattedDate)

 

Output:

19 Hours|23 Minutes|20 Seconds|000000 MicroSeconds

19

07

23

20

000000

Example5: Format codes for getting time zone information from datetime instance

import datetime

 

# Create a timezone instance using timedelta

estTimeDelta    = datetime.timedelta(hours=-5)

tzObject        = datetime.timezone(estTimeDelta, name="EST")

 

# Create a naive object

todaysdate = datetime.datetime.today()

 

#Make the naive object into an aware object by specifying the tzinfo parameter

todaysdate = todaysdate.replace(tzinfo=tzObject)

print(todaysdate)

 

# Print the timezone name

component = todaysdate.strftime("%Z")

print("Time zone:{}".format(component))

 

# Print the UTC Offset of the timezone

component = todaysdate.strftime("%z")

print("UTC offset of Time zone:{} hours".format(component))

 

Output:

2017-02-16 13:53:09.052039-05:00

Time zone:EST

UTC offset of Time zone:-0500 hours

 


Copyright 2024 © pythontic.com