How to Get the Current Time in Python
Time and date values are essential tools for developers. For example, they enable you to capture accurate information about when an activity occurs. This could be as simple as an event log storing sensitive information such as user data.
This post will introduce several Python techniques for fetching the current time based on a given time zone.
Python Fetch Current Time - Time Module
One of Python’s most common and simplest methods of fetching time information is using the built-in time module. This module has functions that allow us to perform time-based operations.
Some functions from this module we can use to determine the current time include:
- The
time
function - The
localtime
function - The
strftime
function - The
ctime
function
The time
Function
We can use the time.time()
function to get the current time in seconds since the epoch as a floating-point value.
The function returns the number of seconds that have elapsed since Jan 1, 1970, 00:00:00 (Epoch).
Example:
import time
c_time = time.time()
print("Current Time (Epoch):", c_time)
Output:
Current Time (Epoch): 1669697416.493398
The localtime
Function
The localtime
function lets us get the current time expressed in seconds since Epoch. Unlike the time
function that returns the value in seconds.microseconds
format, the function returns the value in struct_time
format.
Example code:
import time
c_time = time.localtime(time.time())
print("Current Time: ", c_time)
Output:
Current Time: time.struct_time(tm_year=2022, tm_mon=11, tm_mday=29, tm_hour=7, tm_min=53, tm_sec=9, tm_wday=1, tm_yday=333, tm_isdst=0)
We can use the resulting value to extract specific information such as the year, month, day, hour, etc.
Example:
import time
c_time = time.localtime(time.time())
print("Year: ", c_time.tm_year)
print("Day: ", c_time.tm_mday)
Output:
Year: 2022
Day: 29
The strftime
Function
Python’s strftime
function converts a given struct_time
or tuple into a string representation. This denotes your current time in your local timezone.
Example code is as shown:
import time
c_time = time.localtime()
c_time = time.strftime("%H:%M:%S", c_time)
print("Current Time: ", c_time)
Output:
Current Time: 07:57:38
In the code above, the format is specified as %H:%M:%S
which can be broken down as:
%H
- Hour in the 24-hour clock as a zero-padded decimal value.%M
- Minute as a zero-padded decimal number.%S
- Seconds as a zero-padded decimal number.
Check our Python’s strftime Function Formatting Cheatsheet in the link below:
The ctime
Function
The ctime
function lets us get the current time in a human-readable format. The function’s output is OS-dependent, representing the current time in the default OS format.
Example code:
import time
c_time = time.ctime(time.time())
print("Curren time: ", c_time)
Output:
Curren time: Tue Nov 29 08:05:41 2022
Python Fetch Current Time - Datetime Module
We can also use the datetime
module in Python’s standard library to work with date and time values. Let us see the methods available to us to fetch the current time.
The now
Function
From the datetime module, we can use the now
function to fetch the current date and time as shown in the exmaple below:
from datetime import datetime
c_time = datetime.now()
print("Current Time: ", c_time)
Output:
Current Time: 2022-11-29 08:11:24.228926
You can also access individual attributes from the resulting value as shown:
from datetime import datetime
c_time = datetime.now()
print("Current Year: ", c_time.year)
print("Current Month: ", c_time.month)
print("Current Day: ", c_time.day)
Output:
Current Year: 2022
Current Month: 11
Current Day: 29
The timezone
Function
We can also use the timezone
function from the datetime
module to fetch the current time in UTC format as shown below:
from datetime import datetime, timezone
c_time = datetime.now(timezone.utc)
print("Current Time(UTC): ", c_time)
Output:
Current Time(UTC): 2022-11-29 05:14:58.776086+00:00
The isotime
function
We can also make use of the isotime
function to get the current time in ISO format, as shown in the example below:
from datetime import datetime
c_time = datetime.now().isoformat()
print("Current Time(ISO): ", c_time)
Output:
Current Time(ISO): 2022-11-29T08:17:13.900845
Python Fetch Current Time - The pytz
Library
In some cases, we may wish to fetch the current time from another timezone, other than our current one. In such as case, we dive into an external library such as pytz
.
Let us start by installing the library:
pip3 install pytz
Once installed, we can import the module and use the timezone
function to create a new timezone object. The function accepts the region name as the argument.
We can then call the datetime.now()
function and pass the timezone object as the argument. An example demonstration is as shown:
from datetime import datetime
import pytz
new_york = pytz.timezone('America/New_York')
los_angeles = pytz.timezone('America/Los_Angeles')
qatar = pytz.timezone('Asia/Qatar')
accra = pytz.timezone('Africa/Accra')
athens = pytz.timezone('Europe/Athens')
print("Current Time in New York: ", datetime.now(new_york))
print("Current Time in Los Angeles: ", datetime.now(los_angeles))
print("Current Time in Qatar: ", datetime.now(qatar))
print("Current Time in Accra: ", datetime.now(accra))
print("Current Time in Athens: ", datetime.now(athens))
Output:
Current Time in New York: 2022-11-29 00:26:37.604987-05:00
Current Time in Los Angeles: 2022-11-28 21:26:37.605056-08:00
Current Time in Qatar: 2022-11-29 08:26:37.605069+03:00
Current Time in Accra: 2022-11-29 05:26:37.605077+00:00
Current Time in Athens: 2022-11-29 07:26:37.605085+02:00
The code should return the current time in various time zones.
Conclusion
In this tutorial, you came across various Python methods for fetching the current time. You discovered methods such as the datetime
and time
modules. You also learned how to use the pytz
external library to fetch the current time from a different timezone.
We hope this tutorial helped you. Our articles are free. Help us maintain our site by sharing this article.