Skip to content

Python print DateTime now | Example code

  • by

First, you have to get Today’s date and time then pass this value into a Python Print() function.

Example code print DateTime now in Python

Use the datetime class of the datetime module to get a DateTime in this task.

import datetime

now = datetime.datetime.now()
print(now)

Output:

Python print DateTime now

More specific you can import the datetime object from the datetime module: Then remove the leading datetime. from all of the above.

from datetime import datetime

now = datetime.now()
print(now)

Another example use time.strftime():

from time import gmtime, strftime

now = strftime("%Y-%m-%d %H:%M:%S", gmtime())
print(now)

Output: 2021-07-28 08:42:08

Current date in different formats

from datetime import date

today = date.today()

d1 = today.strftime("%d/%m/%Y")
print(d1)

d2 = today.strftime("%B %d, %Y")
print(d2)

d3 = today.strftime("%m/%d/%y")
print(d3)

d4 = today.strftime("%b-%d-%Y")
print(d4)

Output:

Python print DateTime example code

Do comment if you have any doubts and suggestions on this python date-time topic.

Note: IDE: PyCharm 2021.3.3 (Community Edition)

Windows 10

Python 3.10.1

All Python Examples are in Python 3, so Maybe its different from python 2 or upgraded versions.

Leave a Reply

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