Skip to content

Instantly share code, notes, and snippets.

@rpdelaney
Created July 30, 2019 01:53
Show Gist options
  • Save rpdelaney/3d4e2fce8bcd6086de5eb231cf0c8def to your computer and use it in GitHub Desktop.
Save rpdelaney/3d4e2fce8bcd6086de5eb231cf0c8def to your computer and use it in GitHub Desktop.
Convert Excel date-time to ISO formatted string in python
#!/usr/bin/env python3
#
import pytz
from xlrd import xldate
TZ_PACIFIC = pytz.timezone("US/Pacific")
def xl_to_iso(xl_date: float, datemode: int = 0) -> str:
"""
Takes a Windows/Excel date-time and returns an ISO formatted string converted to Pacific timezone
datemode – 0: 1900-based, 1: 1904-based.
See:
* https://xlrd.readthedocs.io/en/latest/dates.html
* https://support.microsoft.com/en-us/help/214326/excel-incorrectly-assumes-that-the-year-1900-is-a-leap-year # noqa
"""
xl_date_utc = xldate.xldate_as_datetime(xl_date, datemode)
xl_date_pst = xl_date_utc.astimezone(TZ_PACIFIC)
return xl_date_pst.isoformat()
# EOF
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment