Created
October 9, 2016 15:20
-
-
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
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Excellent! Was searching this exact piece of code, thank you a lot!