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 datetimemrx = datetime.datetime.today() print(mrx)

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

Example: 

import datetimemrx = 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 datetimemrx = 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 datetimemrx = 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 datetimemrx = datetime.datetime(2023, 1, 11) print(mrx.strftime("%B")) print(mrx.strftime("%A"))

The following is a list of all the valid format codes:

DirectiveOverviewExampleExecution
%aShort version for weekdayMonTry it
%AComplete version, weekdayMondayTry it
%wA weekday is a number between 0 and 6 and 0 is a Sunday1Try it
%d01-31 is the 31st day of the month09Try it
%bShort form of the month nameJanTry it
%BComplete form of the monthJanuaryTry it
%mThe month is represented by the number 01-1201Try it
%yA short representation of the year without the century23Try it
%YThe complete form of the year2023Try it
%HHour 00-2314Try it
%IHour 00-1202Try it
%pAM/PMPMTry it
%MMinute 00-5956Try it
%SSecond 00-5932Try it
%fMicrosecond 000000-999999398654Try it
%zUTC offset+0600
%ZTimezoneCST
%j001-366 is the day number of the year365Try it
%UThe week number of the year, Sunday as the first day of the week, 00-5352Try it
%WThe number of the week, Monday as the first day of the week, 00-5352Try it
%cDate and time in local time zoneFri Jan 13 16:12:23 2023Try it
%xDate in local version01/13/2023Try it
%XTime in local version16:24:00Try it
%%A % character%Try it
%GISO 8601 year2023Try it
%uISO 8601 weekday (1-7)1Try it
%VISO 8601 weeknumber (01-53)01Try 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 *