Sometimes, we want to print a date in a regular format with Python.
In this article, we’ll look at how to print a date in a regular format with Python.
How to print a date in a regular format with Python?
To print a date in a regular format with Python, we can use the strftime method.
For instance, we write
import datetime
print(datetime.datetime.now().strftime("%Y-%m-%d %H:%M"))
to call strtftime method of the datetime that has the current datetime as its value.
We call it with a string to specify how it’s formatted.
%Y adds the 4 digit year.
%m adds the 2 digit month.
%d adds the 2 digit day of the month.
%H adds the 2 digit hour.
And %M adds the 2 digit minutes.
Conclusion
To print a date in a regular format with Python, we can use the strftime method.