How to Get Formatted Date and Time in Python?

Introduction:

Here we are learning to get formatted date and time. Different date formats are used in many parts of the world, so most programming languages provide different date formats for developers to work with. In Python, this is done using a liberty, which is known as DateTime. It contains classes and methods that can be used to manipulate dates and times. For using this module, we must first import it using the following keywords -

Using the strftime() Function:

The strftime() function is used to returns a formatted date and time. It accepts a string type that you can use to get the results you want. The following are the commands that it supports.

SL. No.DirectivesMeaning of directives
1%aShort name of the field's weekday.
2%BGet the full month's name of the locales.
3%cRepresent the appropriate date and time of the locales.
4%dRepresent the day of the month as an integer [01,31].
5%HRepresent the 24-hour clock as a decimal number [00,23].
6%IRepresent the 12-hour clock as a decimal number [01,12].
7%MRepresent the month as a decimal number [00,59].
8%mRepresent the month as a decimal number [01,12].
9%pLocale is equivalent to AM or PM.
10%SRepresent the second clock as a decimal number [00,61].
11%UThe number of the week in the year as a whole number (Sunday is the first day of the week) [00,53].
12%WThe number of the week in the year as a whole number (Monday is the first day of the week) [00,53]. Every day before the first Monday of the new year is considered week 0.
13%wRepresent the weekday as a decimal number like [0(Sunday),6(Saturday)].
14%XThe time is appropriate to represent the Locale.
15%xThe date is appropriate to represent the Locale.
16%YRepresent the year with century as a decimal number.
17%yRepresent the year without century as a decimal number [0,99].
18%ZRepresents the time zone name (unsigned if there is no time zone).
19%%Represent the literal "%" character.

Using the strftime() Function of the datetime Module:

Given a date, time, or datetime object in the strftime() method returns a string which is representing the date and time. For example, the code %d-%m-%Y%H:%M:%S converts the date to dd-mm-yyyy hh:mm:ss format.

Program Code:

Here we give a program code to get formatted date and time by using the strftime() function in Python. The code is given below -

Output:

Now we run the above code and find the result from it. If the program is run in the online editor, then it will display the GMT. If it is run in the local system editor then it will display the time itself. The result is given below -

 
The current year is: 2024
The current month is: 05
The current day is: 01
The current time is: 16:33:02
The current date and time is: 05/01/2024, 16:33:02   

Using the Time Class:

The time class represents time values. Its attributes are hours, minutes, seconds, and microseconds.

Syntax:

The syntax of the time class is given below -

Program Code 1:

Here we give a program code to get formatted time by using the time class in Python. The code is given below -

Output:

Now we run the above code and find the result from it. The output is given below -

 
The formatted time is: 04:10:20.000020   

Program Code 2:

Here we give another program code to get formatted time by using the time class in Python. There are many examples of the time attribute ranges. Time attribute examples have three properties: hour, minute, second, and microsecond. These are used to obtain specific information over time. The code is given below -

Output:

Now we run the above code and find the result from it. The output is given below -

 
Time is: 4 hour 10 minute 20 second and 20 microsecond   

Using the Date Class:

The date class represents calendar date values. Date examples include year, month, and day properties.

Syntax:

The syntax of the date class is given below -

Program Code:

Here we give a program code to get a formatted date by using the date class in Python. The code is given below -

Output:

Now we run the above code and find the result from it. The output is given below -

 
The Date is: 5 day, 5 month and 2000 year   

Convert the Date to String:

Since dates and times are not the same as strings. So, it is necessary to convert dates and times to strings. We use the strftime() function for this.

Syntax:

The syntax to convert the date to string is given below -

Parameters:

The parameters to convert the date to a string are given below -

  • format: It is a string type. Instructions can be placed in the format string.
  • t: It represent the formatted time.

Program Code:

Here we give a program code to convert the date to string. In this code, the characters %H, %M, and %S represent hours, minutes, and seconds respectively. On the other hand, the characters %b, %d, and %Y representing the month, day, and year respectively. The output is given below -

Output:

Now we run the above code and find the result from it. The output is given below -

 
The formatted date and time is: May 05 2000 04:29:40   

Convert the String to Date:

When working with datasets imported from CSV or input from website files, it is often necessary to convert from string to date. Python has a function called strptime() that converts the string to date.

Syntax:

The syntax to convert the string to date is given below -

Parameters:

The parameters to convert the string to date are given below -

  • string: It represent the formatted string.
  • format: It is a string type. Instructions can be placed in the format string.

Program Code:

Here we give a program code to convert the string into the date-month-year format in Python. The output is given below -

Output:

Now we run the above code and find the result from it. The output is given below -

 
The formatted date and time is:  2000-05-05 00:00:00   

Conclusion:

Here we learn to format a date and time using the datetime module. We also learn how to format a date by converting it to a string and returning it to the date. We also learn some program code.