Python Program to get the current date and time – 2022

Machine Learning Projects

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() the method. So without any further due, let’s do it…

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.
DirectiveDescriptionExample
%aWeekday, short versionMon
%AWeekday, full versionMonday
%wWeekday as a number 0-6, 0 is Sunday5
%dDay of month 01-3124
%bMonth name, short versionJan
%BMonth name, full versionJanuary
%mMonth as a number 01-1211
%yYear, short version, without century22
%YYear, full version2022
%HHour 00-2322
%IHour 00-1207
%pAM/PMAM
%MMinute 00-5946
%SSecond 00-5903
%fMicrosecond 000000-999999228233
%zUTC offset+0530
%ZTimezoneCST
%jDay number of year 001-366365
%UWeek number of year, Sunday as the first day of week, 00-5352
%WWeek number of year, Monday as the first day of week, 00-5352
%cLocal version of date and timeMon Jan 31 07:46:03 2022
%CCentury20
%xLocal version of date12/31/22
%XLocal version of time07:46:03
%%A % character%
%GISO 8601 year2022
%uISO 8601 weekday (1-7)1
%VISO 8601 weeknumber (01-53)01

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 datetime

now = 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 datetime

now = datetime.now().time() # time object

print("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 time

t = 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 projectsdeep learning projectscomputer vision projectsNLP projectsFlask projects at machinelearningprojects.net

Leave a Comment

Your email address will not be published.