Converting datetime to string in Python

  • Sometimes it may be required to convert a datetime in python to string formats for scenarios like storing the date in a JSON file. Python datetime library has a function named strftime() to do this.

    Dates can be converted to any of the required formats using Format Codes. Full Format Codes List provided at the end of this topic.
    Samples provided below for most of the common scenarios.

    Also refer Convert String to Datetime - All Formats for converting a string to datetime object in different formats.

  1. dd-mon-yy

    from datetime import datetime
    strdate = datetime(2022, 1, 15, 20, 35, 45, 123456)
    print(strdate)
    stringdate = strdate.strftime('%d-%b-%y')
    print(stringdate)
    
    Output:
      15-Jan-22
  2. dd-mmm-yyyy

    from datetime import datetime
    strdate = datetime(2022, 1, 15, 20, 35, 45, 123456)
    stringdate = strdate.strftime('%d-%b-%Y')
    print(stringdate)
    
    Output:
      15-Jan-2022
  3. dd-month-yy

    from datetime import datetime
    strdate = datetime(2022, 1, 15, 20, 35, 45, 123456)
    stringdate = strdate.strftime('%d-%B-%y')
    print(stringdate)
    
    Output:
      15-January-22
  4. dd-month-yyyy

    from datetime import datetime
    strdate = datetime(2022, 1, 15, 20, 35, 45, 123456)
    stringdate = strdate.strftime('%d-%B-%Y')
    print(stringdate)
    
    Output:
      15-January-2022
  5. mm/dd/yy

    from datetime import datetime
    strdate = datetime(2022, 1, 15, 20, 35, 45, 123456)
    stringdate = strdate.strftime('%m/%d/%y')
    print(stringdate)
    
    Output:
      01/15/22
  6. mm/dd/yyyy

    from datetime import datetime
    strdate = datetime(2022, 1, 15, 20, 35, 45, 123456)
    stringdate = strdate.strftime('%m/%d/%Y')
    print(stringdate)
    
    Output:
      01/15/2022
  7. dd.mm.yy

    from datetime import datetime
    strdate = datetime(2022, 1, 15, 20, 35, 45, 123456)
    stringdate = strdate.strftime('%d.%m.%y')
    print(stringdate)
    
    Output:
      15.01.22
  8. dd.mm.yyyy

    from datetime import datetime
    strdate = datetime(2022, 1, 15, 20, 35, 45, 123456)
    stringdate = strdate.strftime('%d.%m.%Y')
    print(stringdate)
    
    Output:
      15.01.2022
  9. yy/mm/dd

    from datetime import datetime
    strdate = datetime(2022, 1, 15, 20, 35, 45, 123456)
    stringdate = strdate.strftime('%y/%m/%d')
    print(stringdate)
    
    Output:
      22/01/15
  10. yyyy/mm/dd

    from datetime import datetime
    strdate = datetime(2022, 1, 15, 20, 35, 45, 123456)
    stringdate = strdate.strftime('%Y/%m/%d')
    print(stringdate)
    
    Output:
      2022/01/15
  11. hh:mm (24 Hour)

    from datetime import datetime
    strdate = datetime(2022, 1, 15, 20, 35, 45, 123456)
    stringdate = strdate.strftime('%H:%M')
    print(stringdate)
    
    Output:
      20:35
  12. hh:mm pm

    from datetime import datetime
    strdate = datetime(2022, 1, 15, 20, 35, 45, 123456)
    stringdate = strdate.strftime('%I:%M %p')
    print(stringdate)
    
    Output:
      08:35 PM
  13. hh:mm:ss.ms

    from datetime import datetime
    strdate = datetime(2022, 1, 15, 20, 35, 45, 123456)
    stringdate = strdate.strftime('%H:%M:%S.%f')
    print(stringdate)
    
    Output:
      20:35:45.123456
  14. mm:ss

    from datetime import datetime
    strdate = datetime(2022, 1, 15, 20, 35, 45, 123456)
    stringdate = strdate.strftime('%M:%S')
    print(stringdate)
    
    Output:
      35:45
  15. dd-mmm-yyyy hh:mm

    from datetime import datetime
    strdate = datetime(2022, 1, 15, 20, 35, 45, 123456)
    stringdate = strdate.strftime('%d-%b-%Y %H:%M')
    print(stringdate)
    
    Output:
      15-Jan-2022 20:35
  16. dd-mmm-yyyy hh:mm:ss.ms

    from datetime import datetime
    strdate = datetime(2022, 1, 15, 20, 35, 45, 123456)
    stringdate = strdate.strftime('%d-%b-%Y %H:%M:%S.%f')
    print(stringdate)
    
    Output:
      15-Jan-2022 20:35:45.123456
  17. dd-month-yyyy hh:mm

    from datetime import datetime
    strdate = datetime(2022, 1, 15, 20, 35, 45, 123456)
    stringdate = strdate.strftime('%d-%B-%Y %H:%M')
    print(stringdate)
    
    Output:
      15-January-2022 20:35
  18. dd-month-yyyy hh:mm:ss.ms

    from datetime import datetime
    strdate = datetime(2022, 1, 15, 20, 35, 45, 123456)
    stringdate = strdate.strftime('%d-%B-%Y %H:%M:%S.%f')
    print(stringdate)
    
    Output:
      15-January-2022 20:35:45.123456
  19. yyyy-mm-dd hh:mm

    from datetime import datetime
    strdate = datetime(2022, 1, 15, 20, 35, 45, 123456)
    stringdate = strdate.strftime('%Y-%m-%d %H:%M')
    print(stringdate)
    
    Output:
      2022-01-15 20:35
  20. yyyy-mm-dd hh:mm:ss.ms

    from datetime import datetime
    strdate = datetime(2022, 1, 15, 20, 35, 45, 123456)
    stringdate = strdate.strftime('%Y-%m-%d %H:%M:%S.%f')
    print(stringdate)
    
    Output:
      2022-01-15 20:35:45.123456
  • Format Codes

    Below are the list of the different Format Codes used for conversion.

    Code Description Sample
    %a Weekday (abbr.) as per locale Sun, Mon (for en US)
    %A Weekday full as per locale Sunday, Monday (for en US)
    %b Month (abbr.) as per locale Jan, Feb (for en US)
    %B Month full as per locale January, February (for en US)
    %c Date and time as per locale Wed Jan 15 20:45:00 2022 (for en US)
    %d Day of the month 00, 01, ..., 31
    %f Micro seconds 000000 to 999999
    %H Hour in 24 hour format 00 to 23
    %I Hour in 12 hour format 01 to 12
    %j Day of the year 001 to 366
    %m Month as number 01 to 12
    %M Minute 00 to 59
    %p AM or PM as per locale AM, PM (for en US)
    %S Seconds 00 to 59
    %U Week number of the year. Sunday as first day of week 00 to 53
    %w Week day number with Sunday as first day (0) 0 to 6
    %W Week number of the year. Monday as first day of week 00 to 53
    %x Date as per locale 01/15/2022 (for en US)
    %X Time as per locale 20:45:00 (for en US)
    %y 2 digit year 00 to 99
    %Y 4 digit year 0001 to 9999
    %z UTC offset as +-HHMM[SS[.ffffff]] +0124, -041025, +010203. 555555
    %Z Time zone UTC, GMT, etc
Absolute Code Works - Python Topics