Check current date and datetime in different format in Python

Check the current date and its format:

from datetime import date

today = date.today()
print("Today's is:", today)


# dd/mm/YY
f1 = today.strftime("%d/%m/%Y")
print("f1 =", f1)

# dd-mm-YY
f2 = today.strftime("%d-%m-%Y")
print("f2 =", f2)

# mm/dd/y
f3 = today.strftime("%m/%d/%y")
print("f3 =", f3)

# DD-MON-YYYY	
f4 = today.strftime("%d-%b-%Y")
print("f4 =", f4)

# character format: month, day and year	
f5 = today.strftime("%B %d, %Y")
print("f5 =", f5)

# eg day date month year 2002
f6 = today.strftime("%A %d. %B %Y")
print("f6 =", f6)

Check the current date time with its format:


from datetime import datetime

# datetime object containing current date and time
now = datetime.now()
print("datetime :", now)

# dd/mm/YY H:M:S
dt_string = now.strftime("%d%m%Y%H%M%S")
print("datetime :", dt_string)

# dd/mm/YY H:M:S
dt_string = now.strftime("%d/%m/%Y %H:%M:%S")
print("datetime :", dt_string)

Use the following abbreviation to make new date time format in python

FormatDate format representation
%a or %AWeekday Name Eg %a : SUN,MON or %A : SUNDAY , MONDAY etc
%wWeekday as Number Eg 0 means Sunday …. 6 means saturday
%dDay of the month Eg 01,02,03 …30,31
%b or %BMonth Name eg %b: JAN,FEB or %B: JANUARY, FEBRUARY
%mMonth in Number eg 01,02,….12
%y or %YYear Eg %y : 01,02…99 or %Y : 0001,0002….9999
%H or %IHours eg %H 00,01…23 or %I : 01,02…12
%pAM or PM
%MMinutes eg 00,01,02….59
%SSeconds eg 00,01,02…59
%fMicrosecond eg. 000000,000001….999999
%z or %Zoffset or Timezone name Eg %z : -0400 or %Z : UTC
%jDay of the year eg 001,002,…..366
%U or %WWeek of year Eg %U consider Sunday as start or %W : consider Monday as start

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.