Python Datetime Format
We are examining Python datetime. The lesson today will use examples to better serve the needs of the learners.
Python Datetime
By importing a module called datetime, we can use dates as date objects in Python. In Python, a date is not a distinct data type.
Show the current date utilizing the datetime module:
Example: 
Here is another way to present the current date through the datetime module:
Example: 
Date Output
Python datetime will look like this when we run the code from the example above:
2023-01-13 12:58:16.897746
In addition to the year, month, and day, the date also includes the hour, minute, second, and microsecond.
In the datetime module, there are several methods to provide information about the date object.
The following are some examples, which you will find out more about later in this chapter.
Get the year, month name, and weekday name by providing the following code:
Example: 
Creating Date Objects
By accessing the datetime() class (constructor) of the datetime module, we can make a date utilizing Python datetime.
To generate a date, the datetime() class needs three parameters: year, month, day.
Make a date object as follows:
Example: 
As well as the time and timezone (hour, minute, second, microsecond, tzone), the datetime() class also accepts optional parameters. They have a default value of 0 (which means no timezone).
strftime() Method
Date objects can be formatted into understandable strings utilizing the Python datetime object.
To determine the format of the returned string, strftime() requires one parameter, format:
The name of the month and day will appear as follows:
Example: 
The following is a list of all the valid format codes:
Directive | Overview | Example | Execution |
---|---|---|---|
%a | Short version for weekday | Mon | Try it |
%A | Complete version, weekday | Monday | Try it |
%w | A weekday is a number between 0 and 6 and 0 is a Sunday | 1 | Try it |
%d | 01-31 is the 31st day of the month | 09 | Try it |
%b | Short form of the month name | Jan | Try it |
%B | Complete form of the month | January | Try it |
%m | The month is represented by the number 01-12 | 01 | Try it |
%y | A short representation of the year without the century | 23 | Try it |
%Y | The complete form of the year | 2023 | Try it |
%H | Hour 00-23 | 14 | Try it |
%I | Hour 00-12 | 02 | Try it |
%p | AM/PM | PM | Try it |
%M | Minute 00-59 | 56 | Try it |
%S | Second 00-59 | 32 | Try it |
%f | Microsecond 000000-999999 | 398654 | Try it |
%z | UTC offset | +0600 | |
%Z | Timezone | CST | |
%j | 001-366 is the day number of the year | 365 | Try it |
%U | The week number of the year, Sunday as the first day of the week, 00-53 | 52 | Try it |
%W | The number of the week, Monday as the first day of the week, 00-53 | 52 | Try it |
%c | Date and time in local time zone | Fri Jan 13 16:12:23 2023 | Try it |
%x | Date in local version | 01/13/2023 | Try it |
%X | Time in local version | 16:24:00 | Try it |
%% | A % character | % | Try it |
%G | ISO 8601 year | 2023 | Try it |
%u | ISO 8601 weekday (1-7) | 1 | Try it |
%V | ISO 8601 weeknumber (01-53) | 01 | Try it |
Python Datetime Format Uses
Here are six important uses of datetime formatting:
- You can format
datetime
objects to display dates and times in a specific format. This is useful when presenting dates and times to users or storing them in a specific format in a database or file. - DateTime formatting is essential when parsing strings that represent dates and times into
datetime
objects. Thestrptime()
method allows you to specify a format string to match the input string and convert it into adatetime
object. - Formatting options like
%s
allow you to generate timestamps, which are representations of dates and times as a numeric value representing the number of seconds since a specific reference point (e.g., January 1, 1970). - You can use formatting options to create custom date representations by combining different format codes. For example, you can display the month name followed by the year or the day of the week along with the day and month.
- Formatting dates in a standard format, such as “YYYY-MM-DD”, enables easy sorting and comparison operations. This is particularly useful when working with date-based data and performing tasks like sorting a list of dates in chronological order.
- Formatting options like
%c
,%x
, and%X
allow you to display dates and times according to the locale-specific conventions. This enables localization and internationalization support, ensuring that dates and times are represented in a format appropriate for a specific language or region.