Search code examples
pythondategoogle-sheetsdate-conversion

Convert Google Sheet date serial value into human readable date


Set-up

I'm reading a Google Sheet in Python through Google Drive API.

I've got a value for a date – a date serial value, which I need to convert into a human readable date.

I have:

date = 42747

I know that this implies that the date is 12-1-2017.


Question

Is there an easy way to convert 42747 to 12-1-2017?

I've seen this question, but it's not answering mine.


Solution

  • This would work:

    import datetime
    def convert_xls_datetime(xls_date):
        return (datetime.datetime(1899, 12, 30)
                + datetime.timedelta(days=xls_date))
    
    print(convert_xls_datetime(42747))