Strftime() Function In Python

Function Name:

strftime

 

Function Signature:

strftime(formatString)

 

Function Overview:

The strftime returns a date string as per the format string supplied.

 

The format string is a format specifier where a lowercase letter or uppercase letter is preceded by a % symbol.

 

Each letter immediately following the % symbol has  a specific meaning as defined by the C formatting codes.

 

Example:

import datetime

 

sampleDate = datetime.date(2017, 6, 1)

 

# Print the 3 Letter Abbreviation for a week day

dateFormatted = sampleDate.strftime("%a")

print("3 Letter Abbreviation for a week day: {}".format(dateFormatted))

 

# Print the whole string - the day of the week

dateFormatted = sampleDate.strftime("%A")

print("The day of the week - whole string: {}".format(dateFormatted))

 

# Print the the day of the week as a decimal number - Sunday is 0 and Saturday is 6

dateFormatted = sampleDate.strftime("%w")

print("Day of the week as a decimal number: {}".format(dateFormatted))

 

# Day of the month as a decimal number - Single digits padded with zero

dateFormatted = sampleDate.strftime("%d")

print("Day of the month as a decimal number: {}".format(dateFormatted))

 

 

# Name of the month as a three letter abbreviation

dateFormatted = sampleDate.strftime("%b")

print("Name of the month as a three letter abbreviation: {}".format(dateFormatted))

 

# Name of the month as a three letter abbreviation

dateFormatted = sampleDate.strftime("%b")

print("Month name being printed as a Three Letter Abbreviation: {}".format(dateFormatted))

 

# Month number - Single digit values padded with zero

dateFormatted = sampleDate.strftime("%m")

print("Month number - Single digit values preceded with a zero: {}".format(dateFormatted))

 

# Year number - Two digit number without century - empty digits preceded with zero

dateFormatted = sampleDate.strftime("%y")

print("Year number printed as a two digit number without century info - empty digits preceded by zeroes: {}".format(dateFormatted))

 

# Year number - Four digit number with century - preceding empty digits padded with zero

dateFormatted = sampleDate.strftime("%Y")

print("Year number printed with century information: {}".format(dateFormatted))

 

# Day of the year - preceding empty digits padded with zero

dateFormatted = sampleDate.strftime("%j")

print("Day of the year printed with padded zeros for preceding empty digits: {}".format(dateFormatted))

 

# Appropriate date representation of the locale

dateFormatted = sampleDate.strftime("%c")

print("The date representation as per the locale: {}".format(dateFormatted))

 

Output:

3 Letter Abbreviation for a week day: Thu

The day of the week - whole string: Thursday

Day of the week as a decimal number: 4

Day of the month as a decimal number: 01

Name of the month as a three letter abbreviation: Jun

Month name being printed as a Three Letter Abbreviation: Jun

Month number - Single digit values preceded with a zero: 06

Year number printed as a two digit number without century info - empty digits preceded by zeroes: 17

Year number printed with century information: 2017

Day of the year printed with padded zeros for preceding empty digits: 152

The date representation as per the locale: Thu Jun  1 00:00:00 2017

 


Copyright 2020 © pythontic.com