Get Current Date and Time with Timezone in Python?

In this Python article, we will discuss how to get the current date and time using the program and different features and examples. Let’s begin.

Before learning how to get the current date and time in python, we must learn the DateTime module in python.

1. How to get the Current Date and Time in Python?

In Python, we do not consider date and time itself to be data type, rather it as a module called datetime. And your guess is right, because of timezones and different formats handling date and time correctly is a complex task.

Python provides different ways to find/get the current date and time. For example, we can use the class date of the datetime module.

We can import the datetime module into the program to use the time and date.

There are some modules that need to be installed and are not bundled with Python, but the datetime module is pre-installed which means it is comes in-built with Python.

datetime.now() gives us the current date and time.  The now() function inside the datetime module returns the current local time and date.

import datetime 
now = datetime.datetime.now() 
print ("Your local current date and time is : ", now)
Output
Your local current date and time is :  2021-08-08 20:30:47.009709

NOTE: The output is not fixed, it will change according to the date and time when you have run the program.

1.1. Different characteristics/attributes of datetime

The function now() returns an object of class datetime. datetime has various characteristics/attributes such as year, month, date, hour, minute, second, and milliseconds.

Let’s implement an example to access different characteristics of datetime object.

import datetime 
time_now = datetime.datetime.now() 

print ("Displaying the characteristic of now() : ")     
print ("Prsesent Year : ",time_now.year)     
print ("Prsesent Month : ",time_now.month)     
print ("Prsesent Day : ",time_now.day)     
print ("Prsesent Hour : ",time_now.hour)     
print ("Prsesent Minute : ",time_now.minute)    
print ("Prsesent Second : ",time_now.second)    
print ("Prsesent Microsecond : ",time_now.microsecond) 
Output
Displaying the characteristic of now() : 
Prsesent Year :  2021
Prsesent Month :  6
Prsesent Day :  27
Prsesent Hour :  3
Prsesent Minute :  47
Prsesent Second :  39
Prsesent Microsecond :  968505

2. How to get current Date and Time for any specific Timezone?

As we saw in the above example, it will take your local timezone if it is not defined. But in real-world problems, we generally need the time to be in a specific timezone and UTC is most commonly used.

Python allows users to get the current date and time of any specific timezone.

To do that, we use the pytz library, this library contains the time for all the timezone. The now() function intakes timezone as input and returns timezone-oriented time.

We can also access the set timezone through time_now_zone.tzinfo as shown in the example below.

Let’s understand its implementation with an example.

import datetime
import pytz

time_now_zone = datetime.datetime.now(pytz.timezone("Asia/Kolkata"))
print("Current Time in the country 'India' is : ", time_now_zone)
print("Set Timezone : ", time_now_zone.tzinfo)
Output
Current Time in the country 'India' is :  2021-06-27 09:42:06.891198+05:30
Set Timezone :  Asia/Kolkata

3. How to Get the Current Date in Different Formats?

In previous articles, we have discussed how to convert the date to a string using different formats and strftime.

Lets have a look at some examples here.

from datetime import datetime
# datetime(year, month, day[, hour[, minute[, second[, microsecond[,tzinfo]]]]])
current = datetime(2021, 1, 10, 23, 30)

# date/month/year format
format1 = current.strftime("%d/%m/%Y")
print("format1 =", format1)

# Nomal text format	
format2 = current.strftime("%B %d, %Y")
print("format2 =", format2)

# month/date/year format
format3 = current.strftime("%m/%d/%y")
print("format3 =", format3)

# Month, day & year	
format4 = current.strftime("%b-%d-%Y")
print("format4 =", format4) 

#date and time
format5 = current.strftime("%Y-%m-%d %H:%M:%S")
print("format5 =", format5) 
Output
format1 = 10/01/2021
format2 = January 10, 2021
format3 = 01/10/21
format4 = Jan-10-2021
format5 = 2021-01-10 23:30:00

Read More: We can also convert a string to a datetime object using strptime


4. Conclusion

Finally, to sum up, in this article we have covered everything about how to get the current time and date in python along with different implementation, we have covered:

  • How to get the current date and time in python? Which library or function is used to get the current date and time
    • Different characteristics of now() function in datetime module
  • How to get current date and time for any specific timezone?
  • How to get the current date in different formats.

Helpful Links

Please follow the Python tutorial series or the menu in the sidebar for the complete tutorial series.

Also for examples in Python and practice please refer to Python Examples.

Complete code samples are present on Github project.

Recommended Books


An investment in knowledge always pays the best interest. I hope you like the tutorial. Do come back for more because learning paves way for a better understanding

Do not forget to share and Subscribe.

Happy coding!! ?

Recommended -

Subscribe
Notify of
guest
0 Comments
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x
Index