- Get Current Date and Time with Timezone in Python?
- 1. How to get the Current Date and Time in Python?
- 1.1. Different characteristics/attributes of datetime
- 2. How to get current Date and Time for any specific Timezone?
- 3. How to Get the Current Date in Different Formats?
- 4. Conclusion
- Recommended —
- Python: Get the Current Date and Time with Timezone
- Setting time zone with the zoneinfo module
- Specifying time zone with the pytz module
- Specifying UTC offset
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?
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 wp-block-preformatted output">Output format1 = 10/01/2021 format2 = January 10, 2021 format3 = 01/10/21 format4 = Jan-10-2021 format5 = 2021-01-10 23:30:00
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
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 —
Get Current Date and Time with Timezone in Python? was last modified: August 8th, 2021 by Garvit Parakh
Python: Get the Current Date and Time with Timezone
In Python, you can get the current date and time in a particular time zone by using the datetime.now() method (recommended) or the datetime.fromtimestamp() method and specify the tz argument to a valid time zone object (an instance of a tzinfo subclass). Explaining things in words is quite vague and confusing; let’s examine the following examples for a better understanding.
Setting time zone with the zoneinfo module
This example works with Python 3.9 or later. We’ll use the zoneinfo module (a concrete implementation of the datetime.tzinfo abstract base class) to easily provide the time zone information.
from datetime import datetime import time from zoneinfo import ZoneInfo # US Pacific Time us_eastern_dt = datetime.now(tz=ZoneInfo("America/New_York")) print(us_eastern_dt) # US Western Time us_western_dt = datetime.fromtimestamp(time.time(), tz=ZoneInfo("America/Los_Angeles")) print(us_western_dt) # Turkey Time turkey_dt = datetime.now(tz=ZoneInfo("Europe/Istanbul")) print(turkey_dt) # South Korea Time korea_dt = datetime.now(tz=ZoneInfo("Asia/Seoul")) print(korea_dt) # India Time india_dt = datetime.now(tz=ZoneInfo("Asia/Kolkata")) print(india_dt) # Vietnam Time vietnam_dt = datetime.now(tz=ZoneInfo("Asia/Ho_Chi_Minh")) print(vietnam_dt)
2023-01-17 05:19:22.257891-05:00 2023-01-17 02:19:22.258190-08:00 2023-01-17 13:19:22.258302+03:00 2023-01-17 19:19:22.258408+09:00 2023-01-17 15:49:22.258500+05:30 2023-01-17 17:19:22.258597+07:00
Note that the results you receive depend on when you run the code. If you want to print out the results in a nicer format, see this article: How to Format Date and Time in Python.
Specifying time zone with the pytz module
If you are working with an old version of Python (prior to 3.9 ), you can set the time zone by using the pytz module. This module needs to be installed first:
from datetime import datetime import time import pytz # Germany time germany_time = datetime.now(pytz.timezone('Europe/Berlin')) print(germany_time) # Egypt time egypt_time = datetime.fromtimestamp(time.time(), tz=pytz.timezone('Africa/Cairo')) print(egypt_time) # Pakistan time pakistan_time = datetime.now(pytz.timezone('Asia/Karachi')) print(pakistan_time) # China time china_time = datetime.now(pytz.timezone('Asia/Shanghai')) print(china_time) # Japan time japan_time = datetime.now(tz=pytz.timezone('Asia/Tokyo')) print(japan_time) # Indonesia time indonesia_time = datetime.fromtimestamp(time.time(), tz=pytz.timezone('Asia/Jakarta')) print(indonesia_time) # Australia time australia_time = datetime.now(tz=pytz.timezone('Australia/Sydney')) print(australia_time)
2023-01-17 11:29:02.126002+01:00 2023-01-17 12:29:02.126031+02:00 2023-01-17 15:29:02.126309+05:00 2023-01-17 18:29:02.126392+08:00 2023-01-17 19:29:02.126546+09:00 2023-01-17 17:29:02.126563+07:00 2023-01-17 21:29:02.126865+11:00
Specifying UTC offset
You can also set a timezone by UTC offset. In my opinion, this method has a small advantage in that you will have a harder time getting typos than the 2 approaches above.
from datetime import datetime, timedelta, timezone # UTC + 5:00 (Pakistan, India, Bangladesh, Nepal, Sri Lanka, and Bhutan time zone) dt1 = datetime.now(tz=timezone(timedelta(hours=5))) print(dt1) # UTC + 7:00 (Vietnam, Thailand, and Indonesia time zone) dt2 = datetime.now(tz=timezone(timedelta(hours=7))) print(dt2) # UTC - 3:00 (Argentina, Brazil, Chile, Paraguay, Uruguay, and Falkland Islands time zone) dt3 = datetime.now(tz=timezone(timedelta(hours=-3))) print(dt3)
2023-01-17 15:35:55.151041+05:00 2023-01-17 17:35:55.151252+07:00 2023-01-17 07:35:55.151259-03:00