Categories
Python Answers

How to print a date in a regular format with Python?

Spread the love

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.

By John Au-Yeung

Web developer specializing in React, Vue, and front end development.

Leave a Reply

Your email address will not be published. Required fields are marked *