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
Format | Date format representation |
%a or %A | Weekday Name Eg %a : SUN,MON or %A : SUNDAY , MONDAY etc |
%w | Weekday as Number Eg 0 means Sunday …. 6 means saturday |
%d | Day of the month Eg 01,02,03 …30,31 |
%b or %B | Month Name eg %b: JAN,FEB or %B: JANUARY, FEBRUARY |
%m | Month in Number eg 01,02,….12 |
%y or %Y | Year Eg %y : 01,02…99 or %Y : 0001,0002….9999 |
%H or %I | Hours eg %H 00,01…23 or %I : 01,02…12 |
%p | AM or PM |
%M | Minutes eg 00,01,02….59 |
%S | Seconds eg 00,01,02…59 |
%f | Microsecond eg. 000000,000001….999999 |
%z or %Z | offset or Timezone name Eg %z : -0400 or %Z : UTC |
%j | Day of the year eg 001,002,…..366 |
%U or %W | Week of year Eg %U consider Sunday as start or %W : consider Monday as start |