I'm trying to convert a long to a 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 weird 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 is it complaining when I explicitly cast it to float? The long is a Unix timestamp stored as a long in mysql.
time.ctime()
gives you the string representation of the time.
it should work with:
datetime.datetime.fromtimestamp(myNumber).strftime('%Y-%m-%d %H:%M:%S')
What you need is simply
datetime.datetime.fromtimestamp(myNumber).strftime('%Y-%m-%d %H:%M:%S')
as time.ctime()
returns a string:
>>> time.ctime()
'Sat May 19 13:46:09 2012'
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With