Python is converted to date

I am trying to convert long to date:

class timeStamp(object): def getDateTime(self,longDate): myNumber = float(longDate) return str(datetime.datetime.fromtimestamp(time.ctime(myNumber)).strftime('%Y-%m-%d %H:%M:%S')) 

But I have a strange error:

  File "./index.py", line 104, in getDateTime return str(datetime.datetime.fromtimestamp(time.ctime(myNumber)).strftime('%Y-%m-%d %H:%M:%S')) TypeError: a float is required 

Why does he complain when I explicitly throw it in a float? The Unix long timestamp is stored as long in mysql.

+6
source share
2 answers

You just need datetime.datetime.fromtimestamp(myNumber).strftime('%Y-%m-%d %H:%M:%S') as time.ctime() returns the string:

 >>> time.ctime() 'Sat May 19 13:46:09 2012' 
+8
source

time.ctime() gives a string representation of time.

It should work with:

 datetime.datetime.fromtimestamp(myNumber).strftime('%Y-%m-%d %H:%M:%S') 
+9
source

Source: https://habr.com/ru/post/916152/


All Articles