-1

I have to convert a MATLAB's datenum to Python's datetime (e.g.2010-11-04 00:03:50.209589). The datenum is represented in milliseconds and the date must be from 2010-11-04 00:00:00 to 2011-06-11 00:00:00.

The following code is as below:

matlab_datenum = 6.365057116950260162e+10
python_datetime = datetime.datetime.fromtimestamp(matlab_datenum / 1e3)
print (python_datetime)

The result is : 1972-01-07 16:42:51.169503

The result is wrong because the date must be from 2010-11-04 to 2011-06-11.

Do you have any idea how to correct the result ?

Thank you for your help

Zoya
  • 1,195
  • 2
  • 12
  • 14
  • Possible duplicate of [Converting Matlab's datenum format to Python](http://stackoverflow.com/questions/13965740/converting-matlabs-datenum-format-to-python) – RaminNietzsche Apr 03 '17 at 13:59
  • it's not duplicated. I don't have an error but i want to improve or to correct a result.. – Zoya Apr 03 '17 at 14:37

1 Answers1

1

The datenum page in the Matlab documentation states:

The datenum function creates a numeric array that represents each point in time as the number of days from January 0, 0000.

Python's datetime module page states the following for fromtimestamp:

Return the local date corresponding to the POSIX timestamp which is 00:00:00 1 January 1970

The two functions are counting from different start points and using different units (days and seconds), hence the discrepancy between your two dates.

peteredhead
  • 2,394
  • 1
  • 15
  • 21