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.
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 -
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 -
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. |
We provides tutorials and interview questions of all technology like java tutorial, android, java frameworks
G-13, 2nd Floor, Sec-3, Noida, UP, 201301, India