Skip to content

Instantly share code, notes, and snippets.

@zduey
Created October 9, 2016 15:20
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save zduey/528e78430b6ae8107ddd05f5752dff77 to your computer and use it in GitHub Desktop.
Save zduey/528e78430b6ae8107ddd05f5752dff77 to your computer and use it in GitHub Desktop.
Convert between Excel serialdate and Python datetime with adjustment for 1900 leap year bug
def xlserialdate_to_datetime(xlserialdate):
"""
Converts a Microsoft Excel serial date to datetime object
Arguments:
-----------
xlserialdate : int
Microsoft Excel serial date
Returns:
--------
converted_date : datetime
Datetime value corresponding to given serial date
"""
excel_anchor = datetime.datetime(1900, 1, 1)
if (xlserialdate < 60):
delta_in_days = datetime.timedelta(days=(xlserialdate - 1))
else:
delta_in_days = datetime.timedelta(days=(xlserialdate - 2))
converted_date = excel_anchor + delta_in_days
return converted_date
@hearues-zueke-github
Copy link

hearues-zueke-github commented Mar 12, 2019

Excellent! Was searching this exact piece of code, thank you a lot!

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