Possible Duplicate:
Fetching datetime from float and vice versa in python
Like many people I switched from Matlab
to Python
. Matlab
represents each date as a number. (Integers
being days since 00-00-0000
, and fractions being time in the day). I believe that python
does the same except off a different start date 0-0-0001
I have been playing around with datetime
, but cant seem to get away from datetime
objects and timedeltas
. I am sure this is dead simple, but how do I work with plain old numbers (floats)?
perhaps as a bit of context:
i bring in a date and time stamp and concatenate them to make one time value:
from datetime import datetime
date_object1 = datetime.strptime(date[1][0] + ' ' + date[1][1], '%Y-%m-%d %H:%M:%S')
date_object2 = datetime.strptime(date[2][0] + ' ' + date[2][1], '%Y-%m-%d %H:%M:%S')
The datetime class provides two methods, datetime.timestamp() that gives you a float
and datetime.fromtimestamp(timestamp) that does the reverse conversion. To get the timestamp corresponding to the current time you have the time.time() function.
Note that POSIX epoch is 1970-01-01.
Try this out:
import time
from datetime import datetime
t = datetime.now()
t1 = t.timetuple()
print time.mktime(t1)
It prints out a decimal representation of your date.
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