- Get Current Time in Python (Different Formats & Timezones)
- Current Time in Python
- Current Time in Milliseconds
- Current Time in Seconds
- Get Current Local Date and Time
- Get Current Timezone
- Get Current Epoch/Unix Timestamp
- Current UTC Time
- Current ISO Time
- Current GMT Time
- Get Current Years, Months, Days, Hours, Minutes, and Seconds
- Further Reading
- How to Get the Current Time in Python with Datetime
- Table of Contents
- How to Get the Current Time with the datetime Module
- Attributes of the datetime.now() Function
- How to Get the Current Time of a Timezone with datetime
- Conclusion
- Python Get Current Time
- How to Get the Current Time with the Datetime Module
- How to Get the Current Time with the Time Module
- Final Thoughts
Get Current Time in Python (Different Formats & Timezones)
For example, here is a simple piece of code to get the current time:
from datetime import datetime now = datetime.now().strftime("%H:%M:%S") print(f"The time is ")
This displays the current time in HH:MM:SS format:
This is a comprehensive guide to getting the current time in Python.
You will learn how to get the current time in different formats and contexts in Python. For example, you’ll learn how to get the current time in milliseconds, in the Epoch format, and how to extract different time components from it.
Current Time in Python
Dealing with time is crucial in some Python programs. To make handling instances of time easier in your projects, Python has some useful libraries like time and datetime.
Read more about using the datetime module in Python.
Let’s take a complete look at how you can get the current time in Python using different timezones, formats, and such.
Current Time in Milliseconds
Milliseconds means one-thousandth (1/1000th) of a second.
To get the current time in milliseconds in Python:
import time t = time.time() t_ms = int(t * 1000) print(f"The current time in milliseconds: ")
The current time in milliseconds: 1635249073607
Current Time in Seconds
To get the current time in seconds in Python:
import time t = time.time() t_s = int(t) print(f"The current time in seconds: ")
The current time in seconds: 1635249118
Get Current Local Date and Time
To get the current local date and time in Python:
- Import the datetime class from the datetime module.
- Ask for the current time by datetime.now().
from datetime import datetime now = datetime.now() print(f"The timestamp is ")
The timestamp is 2021-10-26 14:53:37.807390
Get Current Timezone
To get the current local timezone in Python:
- Import the datetime class from the datetime module.
- Get the current time.
- Convert the current time to time that stores timezone information.
- Get your local timezone from the timezone information.
from datetime import datetime now = datetime.now() now_with_tz = now.astimezone() my_timezone = now_with_tz.tzinfo print(my_timezone)
I am in the EEST timezone (Eastern European Summer Time), so the above code gives me:
Get Current Epoch/Unix Timestamp
The epoch time/UNIX time refers to how many seconds have passed since Jan 1st, 1970.
To get the current epoch/UNIX timestamp in Python:
import time t_epoch = time.time() print(f"The epoch time is now: ")
The epoch time is now: 1635251715.9572039
Current UTC Time
To get the current time in UTC time format in Python:
from datetime import datetime, timezone now = datetime.now(timezone.utc) print(f"Current UTC Time: ")
Current UTC Time: 2021-10-26 12:01:54.888681+00:00
Current ISO Time
The ISO 8601 (International Organization for Standardization) is a standard way to express time.
ISO 8601 follows the format T[hh]:[mm]:[ss].
To get the current time in ISO format in Python:
- Import the datetime class from the datetime module.
- Convert current time to ISO format.
from datetime import datetime ISO_time = datetime.now().isoformat() print(f"Current DateTime in ISO: ")
Current DateTime in ISO: 2021-10-26T15:02:36.635934
Current GMT Time
The GMT or Greenwich Mean Time is the solar time observed in Greenwich, London.
To get the current GMT time in Python:
- Import gmttime, strftime from the time module.
- Get the GMT time with the gmttime() function.
- Format the time.
from time import gmtime, strftime now_GMT = strftime("%a, %d %b %Y %I:%M:%S %p %Z", gmtime()) print("GMT time is now: " + now_GMT)
GMT time is now: Tue, 26 Oct 2021 12:10:09 PM UTC
Get Current Years, Months, Days, Hours, Minutes, and Seconds
To extract years, months, days, hours, minutes, and seconds in Python:
- Import the datetime class from the datetime module.
- Get the current date object.
- Pull the time components from the current date object.
from datetime import datetime now = datetime.now() print(f"Year: ") print(f"Month: ") print(f"Day: ") print(f"Hour: ") print(f"Minute: ") print(f"Second: ")
Year: 2021 Month: 10 Day: 26 Hour: 15 Minute: 13 Second: 48
This completes the guide! I hope you find it useful and learned something new. Thanks for reading. Happy coding!
Further Reading
How to Get the Current Time in Python with Datetime
Kolade Chris
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 datetime .
With the datetime module, you can get the current date and time, or the current date and time in a particular time zone.
In this article, I will show you how to get the current time in Python with the datetime module. I will also show you how to get the current time in any timezone of the world.
Table of Contents
How to Get the Current Time with the datetime Module
The first thing you have to do is to import the datetime module like this:
from datetime import datetime
The next thing you can do to quickly get the current date and time is to use the 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
To get the current time in particular, you can use the 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’s pytztimezone() method, passed into it the exact timezone of New York as a string, and assigned it to a variable named newYorkTz (for New York timezone)
- To get the current time in New York, I used the datetime.now() function from the datetime 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 the timeInNewYork variable and stored it in a variable named currentTimeInNewYork , 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.
Kolade Chris
Web developer and technical writer focusing on frontend technologies. I also dabble in a lot of other technologies.
If you read this far, tweet to the author to show them you care. Tweet a thanks
Learn to code for free. freeCodeCamp’s open source curriculum has helped more than 40,000 people get jobs as developers. Get started
freeCodeCamp is a donor-supported tax-exempt 501(c)(3) charity organization (United States Federal Tax Identification Number: 82-0779546)
Our mission: to help people learn to code for free. We accomplish this by creating thousands of videos, articles, and interactive coding lessons — all freely available to the public. We also have thousands of freeCodeCamp study groups around the world.
Donations to freeCodeCamp go toward our education initiatives, and help pay for servers, services, and staff.
Python Get Current Time
Kolade Chris
In your websites and applications, you might want to add functionalities like timestamps or checking the time of a user’s activity.
Every programming language has modules or methods for working with time, and Python is not an exception.
With the datetime and time modules of Python, you can get the current date and time, or the date and time in a particular time zone.
In this article, I will show you how to get the current time in Python with the datetime and time modules.
How to Get the Current Time with the Datetime Module
The first thing you can do to quickly get the current date and time is to use the datetime.now() function from the datetime module:
from datetime import datetime current_date_and_time = datetime.now() print("The current date and time is", current_date_and_time) # The current date and time is 2022-07-12 10:22:00.776664
This shows you not just the time but also the date.
To extract the time, you can use the strftime() function and pass in («%H:%M:%S»)
from datetime import datetime time_now = datetime.now() current_time = time_now.strftime("%H:%M:%S") print("The current date and time is", current_time) # The current date and time is 10:27:45
You can also re-write the code like this:
from datetime import datetime time_now = datetime.now().strftime("%H:%M:%S") print("The current date and time is", time_now) # The current date and time is 10:30:37
How to Get the Current Time with the Time Module
Apart from the datetime() module, the time module is another built-in way to get the current time in Python.
As usual, you have to import the time module first, and then you can use the ctime() method to get the current date and time.
import time current_time = time.ctime() print(current_time) # Tue Jul 12 10:37:46 2022
To extract the current time, you also have to use the strftime() function:
import time current_time = time.strftime("%H:%M:%S") print("The current time is", current_time) # The current time is 10:42:32
Final Thoughts
This article showed you two ways you can get the current time with Python.
If you’re wondering which to use between the time and datetime modules, it depends on what you want:
- time is more precise than datetime
- if you don’t want ambiguity with daylight savings time (DST), use time
- datetime has more built-in objects you can work with but has limited support for time zones.
If you want to work with time zones, you should consider using the pytz module.
To learn how you can get the time in a particular zone, I wrote about the pytz module here.