Let’s say we want to print today’s date or calculate the age of a person. In Python, we can do so easily using the datetime module.
The datetime module can be imported in Python in the following way:
import datetime
Now, let’s say we want to print today’s date and time. We can do so in the following way:
import datetime now = datetime.datetime.now() print(now)
The program will print output like the following:
2021-12-15 13:16:26.109010
Here, 2021-12-15 indicates the year, month, and date. 13:16:26 indicates the hour, minute, and second. And, 109010 indicates the microsecond.
Now, let’s say we want the date and time in a string format and in a more human-readable format. We can use the strftime() function for that purpose. The strftime(format) function takes a format as input and prints the date in the given string format.
For example,
0 Comments