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 the Datetime module in Python
# print current date and time 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 thedatetime
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
# print current date and time in different formats 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 thedatetime
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.
Directive | Description | Example |
%a | Weekday, short version | Mon |
%A | Weekday, full version | Monday |
%w | Weekday as a number 0-6, 0 is Sunday | 5 |
%d | Day of month 01-31 | 24 |
%b | Month name, short version | Jan |
%B | Month name, full version | January |
%m | Month as a number 01-12 | 11 |
%y | Year, short version, without century | 22 |
%Y | Year, full version | 2022 |
%H | Hour 00-23 | 22 |
%I | Hour 00-12 | 07 |
%p | AM/PM | AM |
%M | Minute 00-59 | 46 |
%S | Second 00-59 | 03 |
%f | Microsecond 000000-999999 | 228233 |
%z | UTC offset | +0530 |
%Z | Timezone | CST |
%j | Day number of year 001-366 | 365 |
%U | Week number of year, Sunday as the first day of week, 00-53 | 52 |
%W | Week number of year, Monday as the first day of week, 00-53 | 52 |
%c | Local version of date and time | Mon Jan 31 07:46:03 2022 |
%C | Century | 20 |
%x | Local version of date | 12/31/22 |
%X | Local version of time | 07:46:03 |
%% | A % character | % |
%G | ISO 8601 year | 2022 |
%u | ISO 8601 weekday (1-7) | 1 |
%V | ISO 8601 weeknumber (01-53) | 01 |
Example 3 – Current Date and Time in Python using the Datetime module
# print current date and time using 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
# print current time using datetime module 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 currentdatetime
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
# print current time using datetime 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
# print current time in different Timezones using datetime and pytz module 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 andpytz
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