Can you coerce the data type to a string to perform this calculation?

date_time_obj = datetime.strptime(str(date), '%Y-%m-%d %H:%M:%S') 
Answer from Paul Wildenhain on Stack Overflow
🌐
Python documentation
docs.python.org › 3 › library › datetime.html
datetime — Basic date and time types
Note that on non-POSIX systems that include leap seconds in their notion of a timestamp, leap seconds are ignored by fromtimestamp(), and then it’s possible to have two timestamps differing by a second that yield identical datetime objects.
🌐
Programiz
programiz.com › python-programming › datetime › strftime
Python strftime() - datetime to string
from datetime import datetime timestamp ... date and time representation. We also recommend you to check Python strptime(). The strptime() method creates a datetime object from a string....
🌐
Pandas
pandas.pydata.org › docs › reference › api › pandas.Timestamp.strftime.html
pandas.Timestamp.strftime — pandas 3.0.2 documentation
Format string to convert Timestamp to string. See strftime documentation for more information on the format string: https://docs.python.org/3/library/datetime.html#strftime-and-strptime-behavior. See also · Timestamp.isoformat · Return the time formatted according to ISO 8601.
🌐
GeeksforGeeks
geeksforgeeks.org › python › convert-date-string-to-timestamp-in-python
Convert Date String to Timestamp in Python - GeeksforGeeks
October 29, 2025 - from datetime import datetime s = "20/01/2020" dt = datetime.strptime(s, "%d/%m/%Y") res = dt.timestamp() print(res) ... Explanation: s is date string, which is parsed into a datetime object dt using the format %d/%m/%Y.
🌐
The New Stack
thenewstack.io › home › convert timestamps to strings like a python pro
Convert Timestamps To Strings Like a Python Pro - The New Stack
July 14, 2025 - Learn practical tips to transform machine-readable timestamps into human-friendly date strings using Python's datetime module.
🌐
Flexiple
flexiple.com › python › python-timestamp
Python Timestamp - Get Current timestamp in Python - Flexiple
In Python, the datetime module provides methods for converting timestamps to string representations. One such method is the strftime() function, which takes a timestamp as input and returns a string formatted according to the specified format.
Find elsewhere
🌐
InfluxData
influxdata.com › home › what is the python time now string?
What Is the Python Time Now String? | InfluxData
November 6, 2023 - When you invoke datetime.now(), you’re not just getting a simple string. Instead, you’re receiving a datetime object packed with information: Year, month, day: Represented in YYYY-MM-DD format. Hour, minute, second: Displayed in HH:MM:SS format.
🌐
The New Stack
thenewstack.io › home › how to convert a timestamp to a string in python
How To Convert a Timestamp to a String in Python - The New Stack
November 21, 2024 - This tutorial shows how to convert Python timestamps into readable date strings using the pandas and datetime Python libraries.
🌐
GeeksforGeeks
geeksforgeeks.org › python › how-to-convert-timestamp-string-to-datetime-object-in-python
How to convert timestamp string to datetime object in Python? - GeeksforGeeks
August 23, 2025 - datetime.strptime() is the most common way to convert a date-time string into a datetime object. You provide the string and its format. ... from datetime import datetime s = "2023-07-21 10:30:45" dt = datetime.strptime(s, "%Y-%m-%d %H:%M:%S") ...
🌐
freeCodeCamp
freecodecamp.org › news › strftime-python-datetime-timestamp-string
strftime – Python DateTime Timestamp String
April 19, 2022 - In this article, we'll talk about the strftime() method provided by the datetime object. This method let us convert date and time objects in Python to their string format.
🌐
Pandas
pandas.pydata.org › docs › reference › api › pandas.Timestamp.html
pandas.Timestamp — pandas 3.0.2 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.
🌐
Saturn Cloud
saturncloud.io › blog › how-to-convert-timestamp-to-string-value-in-python-pandas-dataframe
How to Convert Timestamp to String Value in Python Pandas Dataframe | Saturn Cloud Blog
December 26, 2023 - To convert a timestamp to a string value in a Pandas dataframe, we can use the strftime() method. The strftime() method is a powerful method that allows us to format datetime objects as strings.
🌐
Pandas
pandas.pydata.org › pandas-docs › stable › reference › api › pandas.Timestamp.strftime.html
pandas.Timestamp.strftime — pandas 3.0.1 documentation
Format string to convert Timestamp to string. See strftime documentation for more information on the format string: https://docs.python.org/3/library/datetime.html#strftime-and-strptime-behavior. See also · Timestamp.isoformat · Return the time formatted according to ISO 8601.
🌐
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 - python import time print(time.time()) # Output: 1722858482.123456 · That long float? That’s your current timestamp. A time string is a human-readable representation of time — like "2025-08-05 14:30:00".
🌐
Codedamn
codedamn.com › news › python
Working with Timestamps in Python
July 3, 2023 - A: You can convert a string into a timestamp in Python using the strptime() 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
🌐
PYnative
pynative.com › home › python › python datetime › timestamp in python
Python Timestamp With Examples – PYnative
December 5, 2021 - from datetime import datetime # timestamp ts = 1617295943.17321 # convert to datetime dt = datetime.fromtimestamp(ts) print("The date and time is:", dt) # output 2021-04-01 22:22:23.173210Code language: Python (python) Run · We can convert the timestamp string using the datetime formatting.
🌐
TestMu AI Community
community.testmuai.com › ask a question
How to convert a timestamp to a string in Python? - Ask a Question - TestMu AI Community
December 15, 2024 - How to Convert Timestamp into String in Python? I have a problem with the following code. I get an error: “strptime() argument 1 must be str, not Timestamp”. I believe I need to convert the date from a timestamp to a s…