Python - Retrieving parts of a datetime object using strftime()

  • We can use built-in strftime() function to get the required part of a given date.
    Samples below.

  • Year

    Year can be fetched as full year or just the last 2 digits.

    from datetime import datetime
    dt = datetime(2022, 1, 1, 14, 00, 00)
    print("Full Year: " + datetime.strftime(dt, '%Y'))
    print("2 digit Year: " + datetime.strftime(dt, '%y'))
    
    Output:
      Full Year: 2022
      2 digit Year: 22
  • Month

    Month can be displayed as a number (01 - 12) or full name or abbreviated name.

    from datetime import datetime
    dt = datetime(2022, 1, 1, 14, 00, 00)
    print("Month as number: " + datetime.strftime(dt, '%m'))
    print("Month (abbr.): " + datetime.strftime(dt, '%b'))
    print("Month name: " + datetime.strftime(dt, '%B'))
    
    Output:
      Month as number: 01
      Month (abbr.): Jan
      Month name: January
  • Days

    Day number of the year (000 - 366), day number of the month (00 - 31) or day number of the week (0 - 6. Sunday is 0).

    from datetime import datetime
    dt = datetime(2022, 5, 20, 14, 00, 00)
    print("Day of year: " + datetime.strftime(dt, '%j'))
    print("Day of month: " + datetime.strftime(dt, '%d'))
    print("Day of week: " + datetime.strftime(dt, '%w'))
    
    Output:
      Day of year: 140
      Day of month: 20
      Day of week: 5
  • Weekday

    Weekday abbreviated or weekday full.

    from datetime import datetime
    dt = datetime(2022, 5, 20, 14, 00, 00)
    print("Weekday (abbr.): " + datetime.strftime(dt, '%a'))
    print("Weekday full: " + datetime.strftime(dt, '%A'))
    
    Output:
      Weekday (abbr.): Fri
      Weekday full: Friday
  • Week number

    Week number of the year with Sunday or Monday as the first day of the week (00 - 53).

    from datetime import datetime
    dt = datetime(2022, 5, 22, 14, 00, 00)
    print("Week number (Sunday as first day): " + datetime.strftime(dt, '%U'))
    print("Week number (Monday as first day): " + datetime.strftime(dt, '%W'))
    
    Output:
      Week number (Sunday as first day): 21
      Week number (Sunday as first day): 20
  • Time

    Hours (12 or 24 hour format), minutes, seconds, microseconds and AM or PM.

    from datetime import datetime
    dt = datetime(2022, 5, 22, 14, 20, 30, 234567)
    print("Hours (12 hour format): " + datetime.strftime(dt, '%I'))
    print("Hours (24 hour format): " + datetime.strftime(dt, '%H'))
    print("Minutes: " + datetime.strftime(dt, '%M'))
    print("Seconds: " + datetime.strftime(dt, '%S'))
    print("Microseconds: " + datetime.strftime(dt, '%f'))
    print("AM or PM: " + datetime.strftime(dt, '%p'))
    
    Output:
      Hours (12 hour format): 14
      Hours (24 hour format): 02
      Minutes: 20
      Seconds: 30
      Microseconds: 234567
      AM or PM: PM
  • Time zone

    Time zone and UTC offset as +-HHMM[SS[.ffffff]]

    import datetime
    dt = datetime.datetime.now(datetime.timezone.utc)
    print("UTC offset: " + datetime.datetime.strftime(dt, '%z'))
    print("Time zone: " + datetime.datetime.strftime(dt, '%Z'))
    
    Output:
      UTC offset: +0000
      Time zone: UTC
Absolute Code Works - Python Topics