Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

convert date to number python [duplicate]

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')
like image 324
reabow Avatar asked Dec 26 '22 11:12

reabow


2 Answers

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.

like image 21
kmkaplan Avatar answered Jan 04 '23 13:01

kmkaplan


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.

like image 126
ATOzTOA Avatar answered Jan 04 '23 13:01

ATOzTOA