Python Forum

Full Version: [SOLVED] Epoch timestamp without milliseconds?
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Hello,

What's a good way to get an Epoch timestamp without the milliseconds?

for row in cur.execute("SELECT id,created,title FROM content"):
	#2004-08-18 19:11:13
	formated_date = datetime.strptime(row["created"],"%Y-%m-%d %H:%M:%S")

	epoch = datetime.timestamp(formated_date)
	#BAD epoch = datetime.datetime.timestamp(formated_date).replace(microsecond = 0)
	
	#2004-08-20 13:15:42 1093000542.0 Some title
	print(row["created"], epoch, row["title"])
Thank you.
I don't see milliseconds in your result. The 1093000542.0 is the number of seconds since the epoch.
Maybe it's not miliseconds.

I'd like to get rid of the trailing ".0"
There is no trailing 0. That is an artifact of printing a float. If you don't want ".0" to be printed, specify a format for printing. Or you could convert epoch to an int.
That was easy :-) I thought it was timestamp() itself.

epoch = int(datetime.timestamp(formated_date))
print(row["created"], epoch, row["title"])
Thank you.
If you don't know, either look it up or ask Python.
from datetime import datetime

epoch = datetime.now().timestamp()
print("epoch is a", type(epoch))
help(datetime.timestamp)
Output:
epoch is a <class 'float'> Help on method_descriptor: timestamp(...) Return POSIX timestamp as float.