Python How to Get the Current Time

How to get the current time in Python

To get the current time in Python:

  1. Import datetime from datetime module.
  2. Call datetime.now to get the current date.
  3. Format the date to only show hours, minutes, and seconds.

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 {now}")

This displays the current time in HH:MM:SS format:

The time is 14:42:24

In this guide, you will learn how to get the current time in different formats and contexts in Python.

Current Time in Python

Milliseconds

Milliseconds means one-thousandth (1/1000) of a second.

To get the current time in milliseconds in Python:

  1. Import time module.
  2. Get the current epoch time.
  3. Multiply the current epoch time by 1000.

For instance:

import time

t = time.time()
t_ms = int(t * 1000)
print(f"The current time in milliseconds: {t_ms}")

Output:

The current time in milliseconds: 1635249073607

Seconds

To get the current time in seconds in Python:

  1. Import time module.
  2. Get the current epoch time.
  3. Convert the epoch time to an integer.

For example:

import time

t = time.time()
t_s = int(t)
print(f"The current time in seconds: {t_s}")

Output:

The current time in seconds: 1635249118

Local Date and Time

To get the current local date and time in Python:

  1. Import the datetime class from the datetime module.
  2. Ask for the current time by datetime.now().

For instance:

from datetime import datetime

now = datetime.now()
print(f"The timestamp is {now}")

Output:

The timestamp is 2021-10-26 14:53:37.807390

Current Timezone

To get the current local timezone in Python:

  1. Import the datetime class from the datetime module.
  2. Get the current time.
  3. Convert the current time to time that stores timezone information.
  4. Get your local timezone from the timezone information.

For example:

from datetime import datetime

now = datetime.now()
now_with_tz = now.astimezone()
my_timezone = now_with_tz.tzinfo

print(my_timezone)

I am at the Eastern European Summer Time, so I get:

EEST

Or as a shorthand:

from datetime import datetime

print(datetime.now().astimezone().tzinfo)

Output:

EEST

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:

  1. Import the time module.
  2. Call the time.time() function.

For example:

import time

t_epoch = time.time()
print(f"The epoch time is now: {t_epoch}")

Output:

The epoch time is now: 1635251715.9572039

UTC Time

To get the current time in UTC time format in Python:

  1. Import datetime and timezone from the datetime module.
  2. Get the current time in UTC timezone.

For example:

from datetime import datetime, timezone

now = datetime.now(timezone.utc)
print(f"Current UTC Time: {now}")

Output:

Current UTC Time: 2021-10-26 12:01:54.888681+00:00

ISO Time

The ISO 8601 (International Organization for Standardization) is a standard way to express time.

The ISO 8601 follows the format T[hh]:[mm]:[ss].

To get the current time in ISO format in Python:

  1. Import the datetime class from the datetime module.
  2. Convert current time to ISO format.

For example:

from datetime import datetime

ISO_time = datetime.now().isoformat()
print(f"Current DateTime in ISO: {ISO_time}")

Output:

Current DateTime in ISO: 2021-10-26T15:02:36.635934

GMT Time

The GMT or Greenwich Mean Time is the solar time observed in Greenwich, London.

To get the current GMT time in Python:

  1. Import gmttime, strftime and the time modules.
  2. Get the GMT time with time.gmttime() function.
  3. Format the time.

For instance:

from time import gmtime, strftime
import time

now_GMT = time.strftime("%a, %d %b %Y %I:%M:%S %p %Z", time.gmtime())
print("GMT time is now: " + now_GMT)

Output:

GMT time is now: Tue, 26 Oct 2021 12:10:09 PM UTC

Years, Months, Days, Hours, Minutes, and Seconds

To extract years, months, days, hours, minutes, and seconds in Python:

  1. Import the datetime class from the datetime module.
  2. Get the current date object.
  3. Pull the time components from the current date object.

For instance:

from datetime import datetime

now = datetime.now()

print(f"Year: {now.year}")
print(f"Month: {now.month}")
print(f"Day: {now.day}")

print(f"Hour: {now.hour}")
print(f"Minute: {now.minute}")
print(f"Second: {now.second}")

Output:

Year: 2021
Month: 10
Day: 26
Hour: 15
Minute: 13
Second: 48

Conclusion

Today you learned how to get the current time in Python in different situations, timezones, and formats.

Thanks for reading. I hope you found what you were looking for.

Happy coding!

Further Reading

Python Tricks

How to Write to a File in Python

The with Statement in Python

Share on facebook
Share on twitter
Share on linkedin

Leave a Comment

Your email address will not be published.