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: 

import datetime mrx = datetime.datetime.today() print(mrx)

Here is another way to present the current date through the datetime module:

Example: 

import datetime mrx = datetime.datetime.now() print(mrx)


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: 

import datetime mrx = datetime.datetime.now() print(mrx.year) print(mrx.strftime("%B")) print(mrx.strftime("%A"))

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: 

import datetime mrx = datetime.datetime(2023, 1, 10, 13, 54, 10) print(mrx)

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: 

import datetime mrx = datetime.datetime(2023, 1, 11) print(mrx.strftime("%B")) print(mrx.strftime("%A"))

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:

  1. 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.
  2. DateTime formatting is essential when parsing strings that represent dates and times into datetime objects. The strptime() method allows you to specify a format string to match the input string and convert it into a datetime object.
  3. 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).
  4. 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.
  5. 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.
  6. 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.
Your reaction is highly appreciated. Please take a moment to share your thoughts or suggestions for the betterment of this site.
We value your feedback.
+1
0
+1
0
+1
0
+1
0
+1
0
+1
0
+1
0

Subscribe To Our Newsletter
Enter your email to receive a weekly round-up of our best posts. Learn more!
icon

Leave a Reply

Your email address will not be published. Required fields are marked *