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).
🌐
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.
🌐
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.
🌐
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.
🌐
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.
🌐
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.
Find elsewhere
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
Top answer
1 of 3
20

The -0600 is the offset from Greenwich Mean Time (GMT). As another SO question already says, time.strptime cannot read timezone offsets, though datetime.strftime can generate them.

As explained at the start of the datetime module's documentation, there are two ways of approaching "time" in python, naive or aware. When all you care about is time inside your system, dealing with naive time/datetime objects is fine (in which case you can strip out the offset as alan suggested). When you need to compare the values inside your system with the real world's notion of time, you have to start dealing with that offset.

The easy way to deal with this is just to use python-dateutil. It has a parse function that will do its best to fuzzily match the date string you pass in to multiple formats and return a workable datetime instance that represents its best guess as to what you meant.

>>> from dateutil.parser import parse
>>> parse('17/Dec/2011:09:48:49 -0600', fuzzy=True)
datetime.datetime(2011, 12, 17, 9, 48, 49, tzinfo=tzoffset(None, -21600))

Normally, having software give its "best guess" is a Bad Thing. In this case, it seems justified if your input formats are stable. Dealing with time in software development is hard, just go shopping.

2 of 3
3

strptime simply matches the modifiers (%d,%b, ...) with the corresponding segments of the string and then converts that matched piece of string to an integer. So in your case, the -0600 only makes it so that your format string matches the input string.

If you want to adjust the time by a specified offset, I would recommend using a datetime object.

>>>s = '17/Dec/2011:09:48:49 -0600'
>>>from datetime import datetime,timedelta
>>>mytime = datetime.strptime(s,"%d/%b/%Y:%H:%M:%S -0600")
>>>dt = timedelta(minutes=6*60)  #6 hours
>>>mytime-=dt
>>>print mytime
2011-12-17 03:48:49
>>>print mytime.hour
3

Also note that since str is a builtin, it is generally not advisable to reassign it.

🌐
nixCraft
cyberciti.biz › nixcraft › howto › python › python get today’s current date and time
Python Get Today's Current Date and Time - nixCraft
August 6, 2024 - This tutorial describes how to find and get current or today's date and time using Python time and datetime modules including date/time in different formats
🌐
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.
🌐
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).
🌐
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.
🌐
Medium
medium.com › @ajaymaurya73130 › python-timestamp-and-time-strings-a-beginners-ultimate-guide-b5f9ea298011
Python Timestamp and Time Strings: A Beginner’s Ultimate Guide | by Ajaymaurya | Medium
August 6, 2025 - Let’s dive in and learn how Python helps you tame time! A timestamp is a numeric representation of a specific point in time.
🌐
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 ...
🌐
GitHub
gist.github.com › julienr › 9b694e79483fb34fcbc2
Python timestamp · GitHub
Python timestamp. GitHub Gist: instantly share code, notes, and snippets.
🌐
Note.nkmk.me
note.nkmk.me › home › python
Get File Timestamp in Python: os.stat, os.path.getmtime
August 9, 2023 - You can get timestamps with the st_atime, st_mtime, and st_ctime attributes of the os.stat_result object. On some Unix systems of FreeBSD family, including macOS, there is also an attribute st_birthtime.
🌐
Python
docs.python.org › 3 › library › logging.html
logging — Logging facility for Python
February 23, 2026 - The key benefit of having the logging API provided by a standard library module is that all Python modules can participate in logging, so your application log can include your own messages integrated with messages from third-party modules.