Python Time Module: A Beginner’s Guide to Dates & Epoch

Have you ever needed to add a timestamp to a file, schedule a task, or simply display the current date and time in your Python program? If so, you’ve quickly run into one of programming’s most common needs: working with time. Manually calculating dates and times can be a headache, but thankfully, Python has a powerful built-in solution that makes it incredibly simple.

In this guide, you’ll learn how to harness the power of Python’s time module. We’ll demystify a fundamental computing concept called “epoch,” and by the end, you’ll be able to get the current time, break it down into its components (like year, month, and hour), and display it in a clean, formatted string. Let’s dive in!

What is the Python Time Module?

Python’s time module is a standard library that provides various functions to work with time-related tasks. Think of it as your program’s personal watch and calendar, all rolled into one. Whether you need to pause your program’s execution or get the current date, the time module is your first stop.

To get started, you simply need to import it at the beginning of your script.

import time

With this single line, a whole suite of time-handling capabilities becomes available. The first and most fundamental function we’ll explore is time.time().

Getting the Current Time with time.time()

When you call time.time(), it doesn’t return a nicely formatted date and time as you might expect. Instead, it returns a single, large number.

import time

current_time = time.time()
print(current_time)

Output (will look something like this):

1743702468.285

So, what is this mysterious number? It’s the number of seconds that have passed since a very specific moment in history: 00:00:00 UTC on January 1, 1970. This moment is known as the “Unix Epoch.”

What is Epoch?

In computing, an “epoch” is a fixed date and time used as a reference point. For most modern systems, including Python, this is January 1, 1970. This standard provides a universal way for computers to represent time as a simple, increasing number (the number of seconds since the epoch). The term “epoch” comes from a Greek word meaning “an important point in history,” and in the world of computing, this date certainly is one!

You can even verify this yourself with a little math. If we take the number of seconds from time.time() and convert it into years, we can see it aligns perfectly.

import time

epoch = time.time()
seconds_in_a_minute = 60
minutes_in_an_hour = 60
hours_in_a_day = 24
days_in_a_year = 365

years_since_epoch = epoch / (seconds_in_a_minute * minutes_in_an_hour * hours_in_a_day * days_in_a_year)
print(years_since_epoch)

Output:

55.79...

Since the current year is 2025, subtracting 1970 gives us 55 years, and the decimal represents the portion of the current year that has already passed. It’s a precise measurement of time!

Converting Epoch to a Readable Time with time.localtime()

While the epoch seconds are useful for calculations, we need a way to convert them into a human-readable format. This is where time.localtime() comes in. This function takes the number of seconds since the epoch and converts it into a structured object that holds all the components of the date and time.

If you pass 0 to time.localtime(), you’ll get the exact moment of the epoch in your local timezone.

import time

epoch_moment = time.localtime(0)
print(epoch_moment)

Output:

time.struct_time(tm_year=1970, tm_mon=1, tm_mday=1, tm_hour=5, tm_min=30, tm_sec=0, tm_wday=3, tm_yday=1, tm_isdst=0)

To get the current time, we pass our epoch variable (which holds the current seconds) to time.localtime().

import time

epoch = time.time()
current_struct = time.localtime(epoch)
print(current_struct)

Output (will look something like this):

time.struct_time(tm_year=2025, tm_mon=3, tm_mday=28, tm_hour=10, tm_min=15, tm_sec=30, tm_wday=4, tm_yday=87, tm_isdst=0)

This struct_time object is like a container holding all the parts of the date and time:

  • tm_year: The year (e.g., 2025)
  • tm_mon: The month (1 to 12)
  • tm_mday: The day of the month (1 to 31)
  • tm_hour: The hour (0 to 23)
  • tm_min: The minute (0 to 59)
  • tm_sec: The second (0 to 59)
  • tm_wday: The day of the week (0 for Monday, 6 for Sunday)

Extracting Individual Date and Time Components

Now that we have our time in a structured object, how do we get just the year or just the month? It’s very straightforward—you use dot notation.

import time

epoch = time.time()
t = time.localtime(epoch)

print("Current year:", t.tm_year)
print("Current month:", t.tm_mon)
print("Current day of the month:", t.tm_mday)
print("Current hour:", t.tm_hour)
print("It is day", t.tm_yday, "of the year.")

Output:

Current year: 2025
Current month: 3
Current day of the month: 28
Current hour: 10
It is day 87 of the year.

This allows you to use any specific part of the date or time in your program’s logic.

Getting a Formatted Date String with time.ctime()

If you want a clean, pre-formatted string representing the current time without dealing with the struct_time object, time.ctime() is the perfect function. The “c” stands for “convert.”

By default, it uses the current epoch time.

import time

formatted_time = time.ctime()
print("The current time is:", formatted_time)

Output:

The current time is: Fri Mar 28 10:15:30 2025

You can also pass a specific number of epoch seconds to get a formatted string for that moment.

import time

formatted_time = time.ctime(0) # Get the formatted time for the epoch
print("The epoch moment was:", formatted_time)

Output:

The epoch moment was: Thu Jan  1 05:30:00 1970

Ready to Go from Basics to Pro?

You’ve just taken a great first step by learning how to manage time in your Python programs—a crucial skill for any developer. This guide covers the fundamentals, but there’s a whole world of Python waiting for you. To build a deep, comprehensive understanding and work on real-world projects, structured learning is key.

If you’re serious about mastering Python, check out our pre-recorded python comprehensive video course.

Conclusion

Working with time in Python doesn’t have to be complicated. By using the built-in time module, you can easily access the current epoch seconds with time.time(), convert them to a structured format with time.localtime() to extract specific components, and generate beautifully formatted strings with time.ctime(). Remember the concept of the epoch—it’s the universal heartbeat of computing. Keep practicing with these functions, and you’ll be handling time like a pro in no time!

Common Questions About the Python Time Module

What is the difference between time.time() and time.ctime()?
time.time() returns the current time as a floating-point number representing seconds since the epoch (January 1, 1970). time.ctime(), without arguments, takes the current epoch time and converts it into a human-readable string like “Fri Mar 28 10:15:30 2025”.

Why does time.localtime(0) not show midnight for me?
time.localtime() converts epoch time to your local timezone. The Unix Epoch is defined as 00:00:00 UTC. If you are in a timezone ahead of UTC (like India Standard Time, which is UTC+5:30), time.localtime(0) will show 5:30 AM on January 1, 1970. To get UTC time, you would use time.gmtime().

What does tm_wday=4 mean in the struct_time object?
The tm_wday attribute represents the day of the week, starting with Monday as 0. Therefore, tm_wday=4 corresponds to Friday.