Python Program to Format time in AM-PM format


In Python, we have some time built-in functions like strftime() and datetime.now() that can be used to find the time in AM/PM format. The format time in AM/PM format uses various applications such as user interface, report & document, data visualization, and event scheduling. We will speak the time of AM when timing between midnight 11:59:00 to 12 noon. In the same way, we can say that the time difference between 12 noon to 11:59:00 midnight is called PM. The abbreviation AM/PM is used to indicate the exact time.

Syntax

The following syntax is used in the example &miinus;

strftime('%I:%M:%S %p')

The strftime() is an in-built function in Python that can be used to represent the time format.

The following format represent in the parameter are −

  • %I − Hours

  • %M − Minutes

  • %S − Seconds

  • %p − AM/PM

datetime.now()

This is an in-built function in Python that can be used to find the current time.

localtime()

This is the built-in method in Python that returns the current time.

Example 1

In the following example, we will start the program by importing all the content of datetime from the module named datetime which will find the local time. Then store the time of 24 hours format in the variable t_str that will be converted into 12 hours format and check whether it is AM or PM. Now we are using the in-built function strftime(). This function accepts two parameters- t_str(the given time) and ‘%H:%M%S’(to set the format of time in hours, minutes, and, seconds). This time format represents the 24 hours format. Next, we again use the strftime() function to set the time format in 12 hours. In the parameter, the %p is used to check the time in AM or PM. Finally, we are printing the result with the help of the variables t_str and t_am_pm.

from datetime import datetime
t_str = '22:45:32'
t_obj = datetime.strptime( t_str, '%H:%M:%S')
#Time format representation
t_am_pm = t_obj.strftime('%I:%M:%S %p')
print("The given time", t_str)
print("The format in (AM/PM):", t_am_pm)

Output

The given time 22:45:32
The format in (AM/PM): 10:45:32 PM

Example 2

In the following example, we will start the program by importing all the content of datetime from the module named datetime which will find the local time. Then we create the variable dt_obj to store the value by getting the current time using a predefined function datetime.now(). Then take the predefined function strftime() to set the time format of 12 hours. This function acts as an object with the variable dt_obj and stores it in the variable ts_am_pm. Finally, we are printing the result with the help of the variables dt_obj and ts_am_pm.

from datetime import datetime
dt_obj = datetime.now()
ts_am_pm = dt_obj.strftime('%I:%M:%S %p')
print("The current time:",dt_obj)
print("The format in (AM/PM):",ts_am_pm)

Output

The current time: 2023-04-18 17:50:01.963742
The format in (AM/PM): 05:50:01 PM

Example 3

In the following example, we will the program with a function named format_time() that takes a datetime object as an argument and returns a string representing the time in AM/PM format. Then the function uses the strftime() method of the datetime object to format the time according to the specified format string '%I:%M %p'. This format specifies the 12 hours (%I) time schedule, with minutes (%M) and an AM/PM indicator(%p).

from datetime import datetime
def p_time(time):
   return time.strftime('%I:%M %p')
time = datetime.now()
time_format = p_time(time)
print("Time in AM/PM:",time_format)

Output

Time in AM/PM: 06:04 PM

Example 4

In the following example, we will first import the module named time that defines the time to set the current time and allows users to format the time using the built-in method. Then initialize the variable named present_time that stores the value by using built-in method localtime(). We then use the strftime method to format the time in AM/PM format using the format string "%I:%M %p". The %I format code represents the hour in 12-hour format, %M represents the minute, and %p represents either AM or PM and is stored in the variable t_format. Finally, we are printing the result with the help of variable t_format.

import time
present_time = time.localtime()
t_format = time.strftime("%I:%M %p", present_time)
print(t_format)

Output

06:18 PM

Conclusion

We saw the inbuilt function strftime() help to represent the format of time in AM or PM. In example 1, we discussed the 24 hours format to convert into the 12 hours format to check whether the time mode is in AM/PM. In example 2, we saw how to initialize the current time by using datetime.now() function. Then using %p we checked the time in AM/PM.

Updated on: 01-Jun-2023

6K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements