Skip to content

Instantly share code, notes, and snippets.

@conradlee
Created December 23, 2012 21:38
Show Gist options
  • Star 6 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save conradlee/4366296 to your computer and use it in GitHub Desktop.
Save conradlee/4366296 to your computer and use it in GitHub Desktop.
How to convert matlab's datenum representation of time to python's datetime representation.
from datetime import datetime, timedelta
matlab_datenum = 731965.04835648148
python_datetime = datetime.fromordinal(int(matlab_datenum)) + timedelta(days=matlab_datenum%1) - timedelta(days = 366)
@wjwxxn
Copy link

wjwxxn commented Mar 22, 2017

Maybe this is simpler:
python_datetime = datetime.timedelta(days=matlab_datenum -366)+datetime.datetime(1,1,1)

@daluu
Copy link

daluu commented Jul 16, 2017

Cross referencing the gist to its matching blog post: https://sociograph.blogspot.com/2011/04/how-to-avoid-gotcha-when-converting.html

@GeodeticCuriosity
Copy link

I have tried the suggestion by conradlee and wjwxxn, but they both do not work for an array of precise ordinal times (e.g. times like 731965.04835648148 in an array).

daluu, the link you gave goes to a blog, which has a comment to come here, which was also a redirection from another post ... the solutions trail is circular, and nothing seems to be working with an array of ordinal times in decimals.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment