Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Impara Date Formats | Working with Dates
Dealing with Dates and Times in Python

book
Date Formats

There are several date formats applied across the world. Also sometimes we want to convert a date into some readable or presentable format. For example, we can rewrite the same date in multiple ways: "01.11.2021", "11/01/2021", "11 Nov, 2021", "2021-11-01", and so on... How can we transform dates so they still will be interpreted by Python?

So, how can we transform the date object into another format? At first, we need to load datetime class from the datetime library within a new line (yes, sounds tautological). Then, you need to apply .strftime() method of datetime class (datetime.strftime()) with a date as the first argument, and pattern as the second. There where the complexity starts. A pattern - is a string, that can be built using the next format codes (the list is not full):

Format codeMeaningExample
%dDay of the month as a zero-padded decimal number01, 02, ..., 30, 31
%mMonth as a zero-padded decimal number01, 02, ..., 11, 12
%yYear without century as a zero-padded decimal number00, 01, 02, ..., 98, 99
%YYear with century as a decimal number0001, 0002, 1999, 2000, 2001, ...
%bMonth as locale’s abbreviated nameJan, Feb, ..., Nov, Dec
%BMonth as locale’s full nameJanuary, February, ..., November, December
%aWeekday as locale’s abbreviated nameSun, Mon, ..., Fri, Sat
%AWeekday as locale’s full nameSunday, Monday, ..., Friday, Saturday

Let's build some examples. For example, we can create a date object representing the 28th of June, 2012. And let's transform it into two forms: "June 28, 2012" and "2012/06/28". Consider the first format: there we need full month name (B), day of the month (%d) and 4-digit year (%Y). So the pattern will look like "%B %d, %Y". In a similar way the second pattern can be built: 4-digit year (%Y), month as a zero-padded decimal number (%m), and day of the month (%d). In this case, the pattern will look like "%Y/%m/%d".

# Load classes from library
from datetime import date
from datetime import datetime

# Create date object
some_date = date(2012, 6, 28)

# Format date into the first format
print("Transformed into the first format date:", datetime.strftime(some_date, "%B %d, %Y"))
# Format date into the first format
print("Transformed into the second format date:", datetime.strftime(some_date, "%Y/%m/%d"))
1234567891011
# Load classes from library from datetime import date from datetime import datetime # Create date object some_date = date(2012, 6, 28) # Format date into the first format print("Transformed into the first format date:", datetime.strftime(some_date, "%B %d, %Y")) # Format date into the first format print("Transformed into the second format date:", datetime.strftime(some_date, "%Y/%m/%d"))
copy
Compito

Swipe to start coding

Given date object (1998-03-11) assigned to variable day. Using .strftime() method format day to format: "11.03.98" and save the result in day_formatted variable.

Soluzione

# Load classes from library
from datetime import date
from datetime import datetime

# Create date object
day = date(1998, 3, 11)

# Format date into another format
day_formatted = day.strftime("%d.%m.%y")
print("Original date:", day)
print("Formatted date:", day_formatted)

Tutto è chiaro?

Come possiamo migliorarlo?

Grazie per i tuoi commenti!

Sezione 1. Capitolo 5
# Load classes from library
from datetime import date
from datetime import datetime

# Create date object
day = date(1998, 3, 11)

# Format date into another format
day_formatted = day.strftime("___")
print("Original date:", day)
print("Formatted date:", day_formatted)
toggle bottom row
some-alt