Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
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

This comment has been minimized.

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
You can’t perform that action at this time.