Home
Head's Up: I'm in the middle of upgrading my site. Most things are in place, but there are something missing and/or broken including image alt text. Please bear with me while I'm getting things fixed.

Format datetime Strings In Python with .strftime()

-- p

Use ``strftime(FORMAT)`` to format a Python datetime as a string. Scroll past this first code block to see examples. The set of available tokens for formatting new strings is further below


-- python

from datetime import datetime

dt = datetime(2022, 3, 7, 16, 2,9)
dt_string = dt.strftime('%Y-%m-%d')

print(dt_string)



-- results/

2022-03-07

-- /results

-- p

** Examples

-- p

Skip the code. It's used to generate the actual examples in the output.


-- python

from datetime import datetime

formats = [
    '%Y-%m-%d',
    '%b. %-d, %Y',
    '%B %-d, %Y',
    '%c',
]

dt = datetime(2022, 3, 7, 16, 2,9)

for format in formats:
    print("###\n")
    print(f"dt.strftime('{format}')\n")
    print(f"#> {dt.strftime(format)}\n")



-- results/

###

dt.strftime('%Y-%m-%d')

#> 2022-03-07

###

dt.strftime('%b. %-d, %Y')

#> Mar. 7, 2022

###

dt.strftime('%B %-d, %Y')

#> March 7, 2022

###

dt.strftime('%c')

#> Mon Mar  7 16:02:09 2022

-- /results

-- p

** Pre-built String Tokens

-- p

| Description | Code | Example                  |
|-------------+------+--------------------------|
| Datetime    | %c   | Mon Sep 30 07:06:05 2013 |
| Date        | %x   | 09/30/13                 |
| Time        | %X   | 07:06:05                 |

-- p

** Date Tokens

-- p

| Description                | Code |   Example |
|----------------------------+------+-----------|
| Weekday name               | %A   |    Monday |
| Weekday abbreviation       | %a   |       Mon |
| Year - Four digits         | %Y   |      2013 |
| Year - Two digits (padded) | %y   |        13 |
| Month name                 | %B   | September |
| Month abbreviation         | %b   |       Sep |
| Month number               | %-m  |         9 |
| Month number (padded)      | %m   |        09 |
| Day of the month           | %-d  |         7 |
| Day of the month (padded)  | %d   |        07 |

-- p

** Date Meta Tokens

-- p

| Description                | Code |   Example |
|----------------------------+------+-----------|
| Day of the year (padded)    | %j  | 273 |
| Day of the year             | %-j | 273 |
| Week of year (padded)       | %U  |  39 |
| Week of year                | %W  |  39 |
| Weekday index (Sunday is 0) | %w  |   1 |

-- p

** Time Tokens

-- p

| Description           | Code | Example |
|-----------------------+------+---------|
| 24hr hour             | %-H  |       7 |
| 24hr hour (padded)    | %H   |      07 |
| 12hr hour             | %-I  |       7 |
| 12hr hour (padded)    | %I   |      07 |
| Minutes               | %-M  |       6 |
| Minutes (padded)      | %M   |      06 |
| Seconds               | %-S  |       5 |
| Seconds (padded)      | %S   |      05 |
| Microseconds (padded) | %f   |  000000 |

-- p

** Time Meta Tokens

-- p

| Description | Code | Example |
|-------------+------+---------|
| AM or PM    | %p   | AM      |
| UTC offset  | %z   |         |
| Time zone   | %Z   |         |

-- p

** Escape Token

-- p

| Description             | Code | Example |
|-------------------------+------+---------|
| A literal '%' character | %%   | %       |

-- p

| Code |                  Example | Description                    |
|------+--------------------------+--------------------------------|
| %a   |                      Mon | Weekday abbreviation           |
| %A   |                   Monday | Weekday full name              |
| %w   |                        1 | Weekday index (Sunday is 0)    |
| %d   |                       07 | Day of the month (padded)      |
| %-d  |                        7 | Day of the month               |
| %b   |                      Sep | Month abbreviation             |
| %B   |                September | Month full name                |
| %m   |                       09 | Month number (padded)          |
| %-m  |                        9 | Month number                   |
| %y   |                       13 | Two digit year (padded)        |
| %Y   |                     2013 | Four digit year                |
| %H   |                       07 | 24hr clock hour (padded)       |
| %-H  |                        7 | 24hr clock hour                |
| %I   |                       07 | 12hr clock hour (padded)       |
| %-I  |                        7 | 12hr clock hour                |
| %p   |                       AM | AM or PM                       |
| %M   |                       06 | Minutes (padded)               |
| %-M  |                        6 | Minutes                        |
| %S   |                       05 | Seconds (padded)               |
| %-S  |                        5 | Seconds                        |
| %f   |                   000000 | Microsecond (padded)           |
| %z   |                          | UTC offset                     |
| %Z   |                          | Time zone                      |
| %j   |                      273 | Day of the year (padded)       |
| %-j  |                      273 | Day of the year                |
| %U   |                       39 | Week of year (padded)          |
| %W   |                       39 | Week of year                   |
| %c   | Mon Sep 30 07:06:05 2013 | Datetime string representation |
| %x   |                 09/30/13 | Date string representation     |
| %X   |                 07:06:05 | Time string representation     |
| %%   |                        % | A literal '%' character        |

-- p

Note that all the names and abbreviations are in the current locale

-- p

- The numbers using ``%-`` to remove zero padding don't work on all platforms.

-- p

- AM/PM is locale dependent

-- p

- TODO: Put in note about what naive references are
- TODO: Clean up these notes
- ``%z`` - UTC offset is in the form +HHMM or -HHMM (empty string if the the object is naive)
- ``%Z`` - Time zone (empty if object is naive)

-- p

- ``%U`` - 39 - Week number of the year (Sunday as the first day of the week) as a zero padded  All days in a new year preceding the first Sunday are considered to be in week 0|

-- p

- ``%W`` - 39 - Week number of the year (Monday as the first day of the week) as a  All days in a new year preceding the first Monday are considered to be in week 0

-- p

- ``%c`` - Mon Sep 30 07:06:05 2013 - Locale’s appropriate date and time representation

-- p

- ``%x`` - 09/30/13 Locale’s appropriate date representation
- `%X `- 07:06:05 Locale’s appropriate time representation


-- python

lines="""
| %a   |                      Mon | Weekday abbreviation           |
| %A   |                   Monday | Weekday full name              |
| %w   |                        1 | Weekday index (Sunday is 0)    |
| %d   |                       07 | Day of the month (padded)      |
| %-d  |                        7 | Day of the month               |
| %b   |                      Sep | Month abbreviation             |
| %B   |                September | Month full name                |
| %m   |                       09 | Month number (padded)          |
| %-m  |                        9 | Month number                   |
| %y   |                       13 | Two digit year (padded)        |
| %Y   |                     2013 | Four digit year                |
| %H   |                       07 | 24hr hour (padded)       |
| %-H  |                        7 | 24hr hour                |
| %I   |                       07 | 12hr hour (padded)       |
| %-I  |                        7 | 12hr hour                |
| %p   |                       AM | AM or PM                       |
| %M   |                       06 | Minutes (padded)               |
| %-M  |                        6 | Minutes                        |
| %S   |                       05 | Seconds (padded)               |
| %-S  |                        5 | Seconds                        |
| %f   |                   000000 | Microseconds (padded)           |
| %z   |                          | UTC offset                     |
| %Z   |                          | Time zone                      |
| %j   |                      273 | Day of the year (padded)       |
| %-j  |                      273 | Day of the year                |
| %U   |                       39 | Week of year (padded)          |
| %W   |                       39 | Week of year                   |
| %c   | Mon Sep 30 07:06:05 2013 | Datetime string representation |
| %x   |                 09/30/13 | Date string representation     |
| %X   |                 07:06:05 | Time string representation     |
| %%   |                        % | A literal '%' character        |
"""

for line in lines.split("\n"):
    parts = line.split("|")
    if len(parts) > 1:
        print(f"|{parts[3]}|{parts[1]}|{parts[2].replace('.','')}|")
results start