The time.time() function returns the number of seconds since the epoch, as a float. Note that “the epoch” is defined as the start of January 1st, 1970 in UTC. So the epoch is defined in terms of UTC and establishes a global moment in time. No matter where on Earth you are, “seconds past epoch” (time.time()) returns the same value at the same moment.

Here is some sample output I ran on my computer, converting it to a string as well.

>>> import time
>>> ts = time.time()
>>> ts
1355563265.81
>>> import datetime
>>> datetime.datetime.fromtimestamp(ts).strftime('%Y-%m-%d %H:%M:%S')
'2012-12-15 01:21:05'
>>>

The ts variable is the time returned in seconds. I then converted it to a human-readable string using the datetime library.

Answer from squiguy on Stack Overflow
🌐
Python documentation
docs.python.org › 3 › library › datetime.html
datetime — Basic date and time types
Because naive datetime objects are treated by many datetime methods as local times, it is preferred to use aware datetimes to represent times in UTC. As such, the recommended way to create an object representing a specific timestamp in UTC is by calling datetime.fromtimestamp(timestamp, tz=timezone.utc).
🌐
Pandas
pandas.pydata.org › docs › reference › api › pandas.Timestamp.html
pandas.Timestamp — pandas 3.0.1 documentation
class pandas.Timestamp(ts_input=<object object>, year=None, month=None, day=None, hour=None, minute=None, second=None, microsecond=None, tzinfo=None, *, nanosecond=None, tz=<object object>, unit=None, fold=None)[source]# Pandas replacement for python datetime.datetime object.
🌐
GeeksforGeeks
geeksforgeeks.org › python › get-current-timestamp-using-python
Get Current Timestamp Using Python - GeeksforGeeks
May 5, 2025 - The datetime module provides the now() function to get the current date and time, and the timestamp() method converts it to a timestamp.
🌐
PYnative
pynative.com › home › python › python datetime › timestamp in python
Python Timestamp With Examples – PYnative
December 5, 2021 - The timestamp() method of a datetime module returns the POSIX timestamp corresponding to the datetime instance. The return value is float. First, Get the current date and time in Python using the datetime.now() method.
🌐
Python
docs.python.org › 3 › library › time.html
time — Time access and conversions
The range really is 0 to 61; value 60 is valid in timestamps representing leap seconds and value 61 is supported for historical reasons.
🌐
Flexiple
flexiple.com › python › python-timestamp
Python Timestamp - Get Current timestamp in Python - Flexiple
A timestamp in Python represents the current time, typically in seconds or milliseconds since the epoch. It's a numeric value used to denote a particular moment in time within Python programming.
🌐
The New Stack
thenewstack.io › home › python: introduction to timestamps and time strings
Python: Introduction to Timestamps and Time Strings - The New Stack
July 27, 2025 - The datetime module calculates the current timestamp for you and is used like this: Using datetime.datetime.now(), Python automatically calculates the timestamp and then automatically converts it to a time string.
Top answer
1 of 10
924

The time.time() function returns the number of seconds since the epoch, as a float. Note that “the epoch” is defined as the start of January 1st, 1970 in UTC. So the epoch is defined in terms of UTC and establishes a global moment in time. No matter where on Earth you are, “seconds past epoch” (time.time()) returns the same value at the same moment.

Here is some sample output I ran on my computer, converting it to a string as well.

>>> import time
>>> ts = time.time()
>>> ts
1355563265.81
>>> import datetime
>>> datetime.datetime.fromtimestamp(ts).strftime('%Y-%m-%d %H:%M:%S')
'2012-12-15 01:21:05'
>>>

The ts variable is the time returned in seconds. I then converted it to a human-readable string using the datetime library.

2 of 10
348

This is for the text form of a timestamp that can be used in your text files. (The title of the question was different in the past, so the introduction to this answer was changed to clarify how it could be interpreted as the time. [updated 2016-01-14])

You can get the timestamp as a string using the .now() or .utcnow() of the datetime.datetime:

>>> import datetime
>>> print datetime.datetime.utcnow()
2012-12-15 10:14:51.898000

The now differs from utcnow as expected -- otherwise they work the same way:

>>> print datetime.datetime.now()
2012-12-15 11:15:09.205000

You can render the timestamp to the string explicitly:

>>> str(datetime.datetime.now())
'2012-12-15 11:15:24.984000'

Or you can be even more explicit to format the timestamp the way you like:

>>> datetime.datetime.now().strftime("%A, %d. %B %Y %I:%M%p")
'Saturday, 15. December 2012 11:19AM'

If you want the ISO format, use the .isoformat() method of the object:

>>> datetime.datetime.now().isoformat()
'2013-11-18T08:18:31.809000'

You can use these in variables for calculations and printing without conversions.

>>> ts = datetime.datetime.now()
>>> tf = datetime.datetime.now()
>>> te = tf - ts
>>> print ts
2015-04-21 12:02:19.209915
>>> print tf
2015-04-21 12:02:30.449895
>>> print te
0:00:11.239980
Find elsewhere
🌐
InfluxData
influxdata.com › home › how to convert timestamp to datetime in python | influxdata
How to Convert Timestamp to DateTime in Python | InfluxData
June 28, 2023 - When you run the above code, you should see the current timestamp from the epoch of January 1, 1970 as shown below: ... Datetimes are a type of data in Python that are used to represent dates and times together. Python has a built-in datetime module that provides classes and functions for working with date times.
🌐
Programiz
programiz.com › python-programming › datetime › timestamp-datetime
Python timestamp to datetime and vice-versa (With Examples)
December 25, 2018 - In Python, we can get timestamp from a datetime object using the datetime.timestamp() method.
🌐
pytz
pythonhosted.org › Moments › api › timestamp.html
The timestamp Module — Moments 2.0 documentation
return the current timestamp object as the number of seconds since the epoch. aka POSIX timestamp aka utime (?) *2009.11.04 13:57:55 http://stackoverflow.com/questions/255035/converting-datetime-to-posix-time ... take a string of the format: YYYYMMDDTHHMMSS (where T is the character ‘T’) return a python datetime object
🌐
Real Python
realpython.com › python-get-current-time
How to Get and Use the Current Time in Python – Real Python
September 25, 2023 - There’s a slight deviation from the ISO 8601 standard in the format that Python uses, though. The standard says that the date and the hour parts of the timestamp should be separated by a T character, but the default datetime object passed through the print() function separates them with a ...
🌐
Codedamn
codedamn.com › news › python
Working with Timestamps in Python
July 3, 2023 - In the above example, the now() function returns the current date and time, and we store this timestamp in the current_timestamp variable. Python's datetime module also allows us to convert timestamps to different formats.
🌐
WsCube Tech
wscubetech.com › resources › python › timestamp-datetime
Python Timestamp to Datetime and Vice-Versa: With Examples
October 1, 2025 - Convert Python timestamp to datetime and vice-versa easily with built-in functions. Learn how to manage date and time conversions in Python effectively.
🌐
Timestamp Converter
timestamp.online › article › how-to-get-current-timestamp-in-python
How To Get Current Timestamp In Python - Code Example
If you want to get timestamp in Python, you may use functions from modules time, datetime, or calendar.
🌐
GeeksforGeeks
geeksforgeeks.org › python › python-datetime-to-integer-timestamp
Datetime to integer timestamp in Python - GeeksforGeeks
July 23, 2025 - We can convert a datetime object to a timestamp using the built-in timestamp() method, and then round or typecast it to get an integer version. In this article, we'll learn how to convert a Python datetime object into an integer timestamp-either in seconds or milliseconds.
🌐
Learn By Example
learnbyexample.org › working-with-timestamps-in-python
Working with Timestamps in Python - Learn By Example
April 23, 2024 - In Python, there are two primary methods for obtaining the current timestamp, both of which provide the number of seconds that have elapsed since the Unix epoch (January 1st, 1970, at 00:00:00 UTC) with high precision.
🌐
Python.org
discuss.python.org › ideas
A "timestamp" method for date objects - Ideas - Discussions on Python.org
January 25, 2022 - datetime.date objects have a fromtimestamp method, but they do not have a .timestamp() method, as datetime.datetime objects have. That seems inconsistent to me. Just as the .fromtimestamp() method truncates the time information given by the timestamp (Unix time), a .timestamp() method would have to implicitly use a time - e.g. 0 hours in the local time zone (as date objects are naive).
🌐
Medium
ntsh-vicky.medium.com › basic-of-python-date-time-91349829971b
Basic Of Python — Date & Time - Nitish Srivastava
November 15, 2023 - epoch_time is the Unix timestamp which represents the number of seconds since January 1, 1970 (UTC). The fromtimestamp() method is used to convert this timestamp into a datetime object, which you can then use in your program as needed.