How to get the current date and time in Python? — with examples

Abhishek Sharma
4 min readSep 17, 2022

--

In this blog, we will learn how to get the current date and time in Python. We will also format the date and time in different formats using strftime() method. So without any further due, let's do it...

Read full blog here — https://machinelearningprojects.net/current-date-and-time-in-python/

Example 1 — Today’s Date using Datetime module in Python

from datetime import date
today = date.today()
print("Today's date -> ", today)

Output

Today's date ->  2022-09-17
  • Here we have used the date class of the datetime module to get today's date.
  • We have created a date object here using date.today() and we are saving it in the today variable. Then we are simply printing our today variable which contains the date today.

Example 2 — Today’s Date in different formats in Python

from datetime import date
today = date.today()
# dd-mm-YY
d1 = today.strftime("%d-%m-%Y")
print("d1 =",d1)
# Textual month, day and year
d2 = today.strftime("%d %B %Y")
print("d2 =",d2)
# mm-dd-yy
d3 = today.strftime("%m-%d-%y")
print("d3 =",d3)
# Month abbreviation, day and year
d4 = today.strftime("%b-%d-%Y")
print("d4 =",d4)
# Day, Month abbreviation and year
d5 = today.strftime("%d-%b-%Y")
print("d5 =",d5)

Output

d1 = 17-09-2022
d2 = 17 September 2022
d3 = 09-17-22
d4 = Sep-17-2022
d5 = 17-Sep-2022
  • Here also we have used the date class of the datetime module to get today's date.
  • Then we are just simply formatting this date object in different formats.
  • Following is a list of all the codes you can use to format your date and time.

Example 3 — Current Date and Time in Python using the Datetime module

from datetime import datetime# datetime object containing current date and time
now = datetime.now()

print("Date and Time Now -> ", now)
# dd/mm/YY H:M:S
dt_string = now.strftime("%d/%m/%Y %H:%M:%S")
print("Date and Time Now 2 -> ", dt_string)

Output

Date and Time Now ->  2022-09-17 11:00:05.263105
Date and Time Now 2 -> 17/09/2022 11:00:05
  • We can use datetime module to print both date and time at the same time also.
  • Simply create a datetime.now() object and apply formatting to it to get the desired results.

Example 4 — Current Time using the DateTime module in Python

from datetime import datetimenow = datetime.now()current_time = now.strftime("%H:%M:%S")
print("Current Time -> ", current_time)

Output

Current Time ->  11:00:05
  • Here we are using the datetime library to get the current time.
  • We used datetime.now() to get the current datetime object.
  • And then simply format it to get the time in the desired format.

Another Way for getting the Current Time

from datetime import datetimenow = datetime.now().time() # time objectprint("Time Now -> ", now)
print("Type of our Time object -> ", type(now))

Output

Time Now ->  11:00:05.318963
Type of our Time object -> <class 'datetime.time'>
  • We used datetime.now().time() to get the current time object.
  • This method will also print milliseconds with our time object.

Example 5 — Current Time using the Time module

import timet = time.localtime()
current_time = time.strftime("%H:%M:%S", t)
print("Time Now -> ", current_time)

Output

Time Now ->  11:00:05
  • Here we are using the time library to get the current time.
  • We simply used the time.localtime() to get the local time.
  • And then we are simply formatting it according to our needs.

Example 6 — Current Time in Different Timezones

from datetime import datetime
import pytz
tz_NY = pytz.timezone('America/New_York')
datetime_NY = datetime.now(tz_NY)
print("Time in NY -> ", datetime_NY.strftime("%H:%M:%S"))
tz_London = pytz.timezone('Europe/London')
datetime_London = datetime.now(tz_London)
print("Time in London -> ", datetime_London.strftime("%H:%M:%S"))

Output

Time in NY ->  01:30:05
Time in London -> 06:30:05
  • We can also get the current time in different timezones of the world using datetime library and pytz library.
  • Here we are creating two timezone objects America/New_York and Europe/London.
  • And then simply extracting time out of it as we have seen earlier in this blog.

And this is how you can use the current date and time in Python

Do let me know if there’s any query while using the current date and time in Python by contacting me via email or LinkedIn.

So this is all for this blog folks, thanks for reading it and I hope you are taking something with you after reading this and till the next time ?…

Check out my other machine learning projects, deep learning projects, computer vision projects, NLP projects, Flask projects at machinelearningprojects.net

--

--