time.time() Function Explained

time.time() returns the current time as a number.

In Python, this number is the current Unix timestamp: the number of seconds since January 1, 1970 UTC. Beginners often use it for two simple tasks:

  • getting the current timestamp
  • measuring how long some code takes to run

If you want a readable date like 2025-04-22 10:30:00, use the Python datetime module overview instead.

Quick example

import time

current_timestamp = time.time()
print(current_timestamp)

start = time.time()
time.sleep(1)
end = time.time()
print(end - start)

time.time() returns the current time as seconds since the Unix epoch. It is often used for simple timing.

Example output:

1712345678.12345
1.00012

What time.time() does

time.time():

  • returns the current time as a float
  • gives the number of seconds since January 1, 1970 UTC
  • returns what is often called a Unix timestamp or epoch time
  • includes fractions of a second in the decimal part

A simple example:

import time

timestamp = time.time()
print(timestamp)
print(type(timestamp))

Possible output:

1712345678.12345
<class 'float'>

This value is useful for storing or comparing times, but it is not easy for humans to read.

Basic syntax

First, import the time module. Then call time.time() with no arguments.

import time

current_time = time.time()
print(current_time)

Key points:

  • you must import time first
  • time.time() takes no arguments
  • store the result in a variable if you want to use it again later

If you used import time, you must call the function as time.time().

This will fail:

import time

print(time())

Because time is the module name, not the function name.

What the return value looks like

The result is usually a large floating-point number, such as:

1712345678.12345

This means:

  • the whole number part is the number of full seconds
  • the decimal part is the fraction of a second

It is not a formatted date string.

If you want only whole seconds, you can convert it with int():

import time

timestamp = time.time()
print(timestamp)
print(int(timestamp))

If you want a readable date and time, look at datetime.now() explained or the Python datetime module overview.

Common use cases

Beginners usually use time.time() for simple tasks like these:

  • get the current timestamp
  • measure how long code takes to run
  • compare two times by subtraction
  • store simple time values in logs or files

Example: getting the current timestamp

import time

timestamp = time.time()
print("Current timestamp:", timestamp)

Example: comparing two times

import time

first = time.time()
time.sleep(2)
second = time.time()

difference = second - first
print("Seconds between times:", difference)

In this example, time.sleep() pauses the program for about 2 seconds, so the difference is about 2.0.

Using time.time() to measure elapsed time

This is one of the most common beginner uses.

The pattern is:

  1. save the start time
  2. run your code
  3. save the end time
  4. subtract start from end

Example:

import time

start = time.time()

for i in range(3):
    print("Working...")
    time.sleep(0.5)

end = time.time()

elapsed = end - start
print("Elapsed time:", elapsed, "seconds")

Possible output:

Working...
Working...
Working...
Elapsed time: 1.5008 seconds

This works well for simple timing examples and quick checks.

If you want to learn more about the module, see the Python time module overview.

Limits beginners should know

time.time() is useful, but it has some limits:

  • the result depends on your system clock
  • it is not the best tool for precise benchmarking
  • it does not give a human-readable date by itself
  • it is not a replacement for delays like time.sleep()

So a good rule is:

  • use time.time() for timestamps and simple elapsed-time checks
  • use datetime for readable dates and times
  • use time.sleep() when you want to pause your program

Common mistakes

Here are some common problems beginners run into with time.time().

Forgetting to import the time module

This causes an error because Python does not know what time means yet.

current = time.time()
print(current)

Fix:

import time

current = time.time()
print(current)

Expecting a formatted date string

This:

import time

print(time.time())

returns a number, not something like 2025-04-22 10:30:00.

If you need a readable date, use the Python datetime module overview.

Confusing time.time() with time.sleep()

These functions do different jobs:

  • time.time() gets the current timestamp
  • time.sleep() pauses the program

Example:

import time

print("Before:", time.time())
time.sleep(1)
print("After:", time.time())

Calling time() directly after import time

This is wrong:

import time

print(time())

This is correct:

import time

print(time.time())

Rounding too early

If you round or convert to int() too soon, you may lose useful timing detail.

Less precise:

import time

start = int(time.time())
time.sleep(1.2)
end = int(time.time())

print(end - start)

More precise:

import time

start = time.time()
time.sleep(1.2)
end = time.time()

print(end - start)

Useful commands to test time.time()

You can test time.time() quickly from your terminal.

Check your Python version:

python --version

Print the current timestamp:

python -c "import time; print(time.time())"

Measure about 1 second of sleep:

python -c "import time; start=time.time(); time.sleep(1); print(time.time()-start)"

Check the return type:

python -c "import time; print(type(time.time()))"

If you are new to troubleshooting, the how to debug Python code beginner guide is a good next step.

FAQ

What does time.time() return in Python?

It returns the current time as a float representing seconds since the Unix epoch.

Why does time.time() return a large number?

Because it counts seconds from January 1, 1970 UTC to the current moment.

Is time.time() good for measuring code speed?

It is fine for simple timing, but more precise tools are better for benchmarking.

How do I get whole seconds from time.time()?

Wrap the result with int() if you only want the integer part.

import time

print(int(time.time()))

See also

Use time.time() when you need a timestamp or a simple elapsed-time check. If you need readable dates, move to datetime. If you need to pause your program, use time.sleep().