In Python 3.2+: datetime.timezone.utc
:
The standard library makes it much easier to specify UTC as the time zone:
>>> import datetime
>>> datetime.datetime.now[datetime.timezone.utc]
datetime.datetime[2020, 11, 27, 14, 34, 34, 74823, tzinfo=datetime.timezone.utc]
You can also get a datetime that includes the local time offset using astimezone
:
>>> datetime.datetime.now[datetime.timezone.utc].astimezone[]
datetime.datetime[2020, 11, 27, 15, 34, 34, 74823, tzinfo=datetime.timezone[datetime.timedelta[seconds=3600], 'CET']]
[In Python 3.6+, you can shorten the last line to: datetime.datetime.now[].astimezone[]
]
If you want a solution that uses only the standard library and that works in both Python 2 and Python 3, see jfs' answer.
In Python 3.9+: zoneinfo
to use the IANA time zone database:
In Python 3.9, you can specify particular time zones using the standard library, using zoneinfo
, like this:
>>> from zoneinfo import ZoneInfo
>>> datetime.datetime.now[ZoneInfo["America/Los_Angeles"]]
datetime.datetime[2020, 11, 27, 6, 34, 34, 74823, tzinfo=zoneinfo.ZoneInfo[key='America/Los_Angeles']]
zoneinfo
gets its database of time zones from the operating system, or from the
first-party PyPI package tzdata
if available.
In your Python applications, you might want to work with time to add functionalities like timestamps, check the time of a user’s activity, and more. One
of the modules that helps you work with date and time in Python is With the In this article, I will show you how to get the current time in Python with the The first thing you have to do is to import the The next thing you can do to quickly
get the current date and time is to use the To get the current time in particular, you can use the datetime
.datetime
module, you can get the current date and time, or the current date and time in a particular time zone.datetime
module. I will also show you how to get the current time in any timezone of the world.Table of Contents
datetime
Moduledatetime.now[]
Functiondatetime
datetime
module like this:from datetime import datetime
datetime.now[]
function from the datetime
module:from datetime import datetime
currentDateAndTime = datetime.now[]
print["The current date and time is", currentDateAndTime]
# Output: The current date and time is 2022-03-19 10:05:39.482383
strftime[]
method and pass into it the string ”%H:%M:%S”
representing hours, minutes, and seconds.
This would give you the current time in a 24Hour format:
from datetime import datetime
currentDateAndTime = datetime.now[]
print["The current date and time is", currentDateAndTime]
# Output: The current date and time is 2022-03-19 10:05:39.482383
currentTime = currentDateAndTime.strftime["%H:%M:%S"]
print["The current time is", currentTime]
# The current time is 10:06:55
Attributes of the datetime.now[]
Function
The datetime.now
function has several attributes with which you can get the year, month,
week, day, hour, minute, and second of the current date.
The snippet of code below prints all the values of the attributes to the terminal:
from datetime import datetime
currentDateAndTime = datetime.now[]
print["The current year is ", currentDateAndTime.year] # Output: The current year is 2022
print["The current month is ", currentDateAndTime.month] # Output: The current month is 3
print["The current day is ", currentDateAndTime.day] # Output: The current day is 19
print["The current hour is ", currentDateAndTime.hour] # Output: The current hour is 10
print["The current minute is ", currentDateAndTime.minute] # Output: The current minute is 49
print["The current second is ", currentDateAndTime.second] # Output: The current second is 18
How to Get the Current Time of a Timezone with datetime
You can get the current time in a particular timezone by using the datetime
module with another module called pytz
.
You can install the pytz
module from pip like this:
pip install pytz
The first thing you have
to do is import both the datetime
and pytz
modules:
from datetime import datetime
import pytz
You can then check for all available timezones with the snippet below:
from datetime import datetime
import pytz
zones = pytz.all_timezones
print[zones]
# Output: all timezones of the world. Massive!
In the snippet of code below, I was able to get the time in New York:
from datetime import datetime
import pytz
newYorkTz = pytz.timezone["America/New_York"]
timeInNewYork = datetime.now[newYorkTz]
currentTimeInNewYork = timeInNewYork.strftime["%H:%M:%S"]
print["The current time in New York is:", currentTimeInNewYork]
# Output: The current time in New York is: 05:36:59
How was I able to get the current time in New York?
- I brought in the
pytz
module’spytztimezone[]
method, passed into it the exact timezone of New York as a string, and assigned it to a variable namednewYorkTz
[for New York timezone] - To get the
current time in New York, I used the
datetime.now[]
function from thedatetime
module and passed into it the variable I created to store the timezone in New York - To finally get the current time in New York in a 24 Hour format, I used the
strftime[]
method on thetimeInNewYork
variable and stored it in a variable namedcurrentTimeInNewYork
, so I can print it to the terminal
Conclusion
As shown in this article, the datetime
module is very handy in working with time and dates,
and subsequently getting the current time in your locale.
When combined with the pytz
module that you can install from pip, you can also use it to get the current time in any timezone of the world.
Thank you for reading. If you find this article helpful, you can share it with your friends and family.
Learn to code for free. freeCodeCamp's open source curriculum has helped more than 40,000 people get jobs as developers. Get started