Python Get Current time#
Learn to get current time of your locale as well as different time zones in Python.
There are a number of ways you can take to get current time in Python.
Example 1: Current time using datetime object#
# Example 1: Current time using datetime object
from datetime import datetime
now = datetime.now()
current_time = now.strftime("%H:%M:%S")
print("Current Time =", current_time)
# When you run the program, the output will be something like below:
Current Time = 18:08:06
Note: In the above example, we have imported the
datetime
class from the datetime module. Then, we usednow
method to get adatetime
object containing current date and time.
Using datetime.strftime() method, we then created a string representing current time.
If you need to create a time
object containing current time, you can do something like this.
# from datetime import datetime
now = datetime.now().time() # time object
print("now =", now)
print("type(now) =", type(now))
# When you run the program, the output will be something like below:
now = 18:08:06.994177
type(now) = <class 'datetime.time'>
Example 2: Current time using time module#
You can also get the current time using time
module.
# Example 2: Current time using time module
import time
t = time.localtime()
current_time = time.strftime("%H:%M:%S", t)
print(current_time)
# When you run the program, the output will be something like below:
18:08:07
Example 3: Current time of a timezone#
If you need to find current time of a certain timezone, you can use pytZ module.
# Example 3: Current time of a timezone
from datetime import datetime
import pytz
tz_NY = pytz.timezone('America/New_York')
datetime_NY = datetime.now(tz_NY)
print("NY time:", datetime_NY.strftime("%H:%M:%S"))
tz_London = pytz.timezone('Europe/London')
datetime_London = datetime.now(tz_London)
print("London time:", datetime_London.strftime("%H:%M:%S"))
NY time: 08:38:07
London time: 13:38:07
Example 4: Time Objects to Represent Time#
# Example 4: Time Objects to Represent Time
from datetime import time
# time(hour = 0, minute = 0, second = 0)
a = time()
print("a =", a)
# time(hour, minute and second)
b = time(10, 30, 50)
print("b =", b)
# time(hour, minute and second)
c = time(hour=10, minute=30, second=50)
print("c =", c)
# time(hour, minute, second, microsecond)
d = time(10, 30, 50, 200555)
print("d =", d)
a = 00:00:00
b = 10:30:50
c = 10:30:50
d = 10:30:50.200555
Example 5: Difference Between Two Points in Time Using#
# Example 5: Difference Between Two Points in Time Using
from datetime import date
today = date(year=2019, month=12, day=5)
new_year = date(year=2020, month=1, day=1)
time_left_for_newyear = new_year - today
# Time left for new year: 27 days, 0:00:00
print('Time left for new year: ', time_left_for_newyear)
t1 = datetime(year = 2019, month = 12, day = 5, hour = 0, minute = 59, second = 0)
t2 = datetime(year = 2020, month = 1, day = 1, hour = 0, minute = 0, second = 0)
diff = t2 - t1
print('Time left for new year:', diff) # Time left for new year: 26 days, 23: 01: 00
Time left for new year: 27 days, 0:00:00
Time left for new year: 26 days, 23:01:00
Example 6: Difference Between Two Points in Time Using timedelata
#
# Example 6: Difference Between Two Points in Time Using `timedelata`
from datetime import timedelta
t1 = timedelta(weeks=12, days=10, hours=4, seconds=20)
t2 = timedelta(days=7, hours=5, minutes=3, seconds=30)
t3 = t1 - t2
print("t3 =", t3)
t3 = 86 days, 22:56:50