Ultimately you want to review the datetime documentation and become familiar with the formatting variables, but here are some examples to get you started:

import datetime

print('Timestamp: {:%Y-%m-%d %H:%M:%S}'.format(datetime.datetime.now()))
print('Timestamp: {:%Y-%b-%d %H:%M:%S}'.format(datetime.datetime.now()))
print('Date now: %s' % datetime.datetime.now())
print('Date today: %s' % datetime.date.today())

today = datetime.date.today()
print("Today's date is {:%b, %d %Y}".format(today))

schedule = '{:%b, %d %Y}'.format(today) + ' - 6 PM to 10 PM Pacific'
schedule2 = '{:%B, %d %Y}'.format(today) + ' - 1 PM to 6 PM Central'
print('Maintenance: %s' % schedule)
print('Maintenance: %s' % schedule2)

The output:

Timestamp: 2014-10-18 21:31:12

Timestamp: 2014-Oct-18 21:31:12

Date now: 2014-10-18 21:31:12.318340

Date today: 2014-10-18

Today's date is Oct, 18 2014

Maintenance: Oct, 18 2014 - 6 PM to 10 PM Pacific

Maintenance: October, 18 2014 - 1 PM to 6 PM Central

Reference link: https://docs.python.org/3.4/library/datetime.html#strftime-strptime-behavior

Answer from Zach Reneau on Stack Overflow
🌐
PYnative
pynative.com › home › python › python datetime › timestamp in python
Python Timestamp With Examples – PYnative
December 5, 2021 - from datetime import datetime # date in string format birthday = "23.02.2012 09:12:00" # convert to datetime instance date_time = datetime.strptime(birthday, '%d.%m.%Y %H:%M:%S') # timestamp in milliseconds ts = date_time.timestamp() * 1000 ...
🌐
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).
🌐
Flexiple
flexiple.com › python › python-timestamp
Python Timestamp - Get Current timestamp in Python - Flexiple
In this example, the fromtimestamp() function converts the given timestamp to a datetime object, representing the date and time associated with that timestamp. Additionally, Python offers flexibility in formatting datetime objects into human-readable strings using the strftime() method.
🌐
Pandas
pandas.pydata.org › docs › reference › api › pandas.Timestamp.html
pandas.Timestamp — pandas 3.0.2 documentation
They can be passed by position ... but not both mixed together. ... >>> pd.Timestamp(1513393355, unit='s', tz='US/Pacific') Timestamp('2017-12-15 19:02:35-0800', tz='US/Pacific')...
🌐
Programiz
programiz.com › python-programming › datetime › timestamp-datetime
Python timestamp to datetime and vice-versa (With Examples)
December 25, 2018 - from datetime import datetime # timestamp is number of seconds since 1970-01-01 timestamp = 1545730073 # convert the timestamp to a datetime object in the local timezone dt_object = datetime.fromtimestamp(timestamp) # print the datetime object and its type print("dt_object =", dt_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.
🌐
Programiz
programiz.com › python-programming › datetime
Python datetime (With Examples)
Python datetime.date Class · Example 3: Date object to represent a date · Example 5: Get the date from a timestamp · Example 6: Print today's year, month and day · Python datetime.time Class · Example 7: Time object to represent time · Example 8: Print hour, minute, second and microsecond ·
Find elsewhere
🌐
Codedamn
codedamn.com › news › python
Working with Timestamps in Python
July 3, 2023 - from datetime import datetime current_timestamp = datetime.now() formatted_timestamp = current_timestamp.strftime('%Y-%m-%d %H:%M:%S') print(formatted_timestamp) In this example, '%Y-%m-%d %H:%M:%S' is the format string.
🌐
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 - Let’s say you want your output to include the Month, Day, Year, Hour, and Minutes (which would be %m-%d-%Y %H:%M). The Python code for this would look like: from datetime import datetime timestamp = datetime.now() formatted_string = ...
🌐
Pandas
pandas.pydata.org › pandas-docs › version › 0.23.4 › generated › pandas.Timestamp.html
pandas.Timestamp — pandas 0.23.4 documentation
This converts a datetime-like string >>> pd.Timestamp(‘2017-01-01T12’) Timestamp(‘2017-01-01 12:00:00’)
🌐
Timestamp Converter
timestamp.online › article › how-to-convert-timestamp-to-datetime-in-python
How To Convert Timestamp To Date and Time in Python - Code Example
import time ts = time.gmtime() print(time.strftime("%Y-%m-%d %H:%M:%S", ts)) # 2026-03-19 07:35:27 print(time.strftime("%x %X", ts)) # 03/19/26 07:35:27 # Iso Format print(time.strftime("%c", ts)) # Thu Mar 19 07:35:27 2026 # Unix timestamp print(time.strftime("%s", ts)) # 1773902127
🌐
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.
🌐
Learn By Example
learnbyexample.org › working-with-timestamps-in-python
Working with Timestamps in Python - Learn By Example
April 23, 2024 - So it’s important to understand the difference between naive and timezone-aware datetime objects when working with timestamps, especially if your application deals with dates and times across multiple timezones. Incorrect handling of timezones can result in errors in how time is interpreted and compared. Let’s illustrate this with an example. The code below demonstrates how Python handles timestamps for both naive and timezone-aware datetime objects.
🌐
Learn By Example
learnbyexample.org › python-datetime-timestamp-method
Python datetime timestamp() Method - Learn By Example
April 15, 2024 - So it’s important to understand the difference between naive and timezone-aware datetime objects when working with timestamps, especially if your application deals with dates and times across multiple timezones. Incorrect handling of timezones can result in errors in how time is interpreted and compared. Let’s illustrate this with an example. The code below demonstrates how Python handles timestamps for both naive and timezone-aware datetime objects.
🌐
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 - Previously, we touched on converting timestamps to datetimes using the fromtimestamp() function. While this function provides the date and time in your system’s local time zone by default, you might need to consider different time zones when working with data from diverse sources. Python’s datetime module allows you to work with time zones using the pytz library. Here’s a quick example of converting a timestamp to a datetime with a specific time zone:
🌐
Dataquest
dataquest.io › blog › python-datetime-tutorial
Python Datetime Tutorial: Manipulate Times, Dates, and Time Spans
January 5, 2026 - Similarly, we can do the reverse conversion using fromtimestamp(). This is a datetime function that takes a timestamp (in float format) as an argument and returns a datetime object, as below:
🌐
TutorialsPoint
tutorialspoint.com › python_pandas › python_pandas_timestamp.htm
Python Pandas - Timestamp
The to_datetime() function creates timestamp from various formats like strings, lists of dates, or integers. The following example creates a Timestamp from a list of strings using the pd.to_datetime() function.
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