Home > Article > Backend Development > Python program to format time in AM-PM format
In Python, we have some built-in time functions such as strftime() and datetime.now() which can be used to find the time in AM/PM format. Time in AM/PM format is used in a variety of applications such as user interfaces, reporting and documentation, data visualization, and event scheduling. When the time is between 11:59:00 midnight and 12 noon, we say AM time. Similarly, we can say that the time between 12 o'clock and 11:59:00 midnight is PM. The abbreviations AM/PM are used to indicate the exact time.
The following syntax is used in the example &miinus;
strftime('%I:%M:%S %p')
strftime() is a built-in function in Python that can be used to represent time format.
The following format is represented in the parameters:
%I − Hours
%M − Minutes
%S − Seconds
%p − AM/PM
datetime.now()
This is a built-in 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.
In the example below we will start the program by importing everything for datetime from a module called datetime which will find the local time. Then store the time in 24 hour format in variable t_str, the time will be converted to 12 hour format and check if it is AM or PM. Now we are using the built-in function strftime(). This function accepts two parameters - t_str (the given time) and '%H:%M%S' (set the format of the time, including hours, minutes and seconds). This time format represents the 24-hour clock. Next, we use the strftime() function again to format the time to 12-hour time. In the parameters, %p is used to check whether the time is AM or PM. Finally, we use the variables t_str and t_am_pm to print the results.
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)
The given time 22:45:32 The format in (AM/PM): 10:45:32 PMThe Chinese translation of
In the example below we will start the program by importing everything for datetime from a module called datetime which will find the local time. Then, we create the variable dt_obj to store the value of the current time obtained by using the predefined function datetime.now(). Then use the predefined function strftime() to set the 12-hour time format. This function will be used as an object with the variable dt_obj and stored in the variable ts_am_pm. Finally, we print the results with the help of 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)
The current time: 2023-04-18 17:50:01.963742 The format in (AM/PM): 05:50:01 PMThe Chinese translation of
In the following example, we will write a program using a function called format_time(), which takes a datetime object as a parameter and returns a string representing the time, displayed in AM/PM format. The function then uses the strftime() method of the datetime object to format the time according to the specified format string '%I:%M %p'. This format specifies a timetable in 12-hour format (%I), including minutes (%M) and AM/PM indicators (%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)
Time in AM/PM: 06:04 PMThe Chinese translation of
In the following example, we first import the module named time, which defines the time to set the current time and allows the user to format the time using built-in methods. Then a variable named present_time that stores the value is initialized by using the built-in method localtime(). We then use the strftime method to format the time into AM/PM format using the format string "%I:%M %p". The %I format code represents the hour in the 12-hour format, %M represents the minute, and %p represents AM or PM, and is stored in the variable t_format. Finally, we print the result using the variable t_format.
import time present_time = time.localtime() t_format = time.strftime("%I:%M %p", present_time) print(t_format)
06:18 PM
We see that the built-in function strftime() can help represent the AM or PM format of time. In Example 1, we discussed the method of converting 24-hour format to 12-hour format to check whether the time mode is AM/PM. In Example 2, we see how to initialize the current time using the datetime.now() function. Then use %p to check if the time is AM/PM.
The above is the detailed content of Python program to format time in AM-PM format. For more information, please follow other related articles on the PHP Chinese website!