Data format being changed when saving .xls file with Python

Question:

TLDR I have dates that show in this format ‘44992’, which I can’t recognise – is there a way to turn that into another format in JS?

I have a Python script that pulls a spreadsheet from a website. In that original spreadsheet, there’s a column with dates in this format: 07/03/2023 00:00:00

There’s an issue with the spreadsheet that two columns have the same name, so I run a function which opens the spreadsheet, changes the name of one of those columns, and then re-saves it. After that, the date is in this format: 44992

I don’t understand why it’s doing that or what format the second number correlates to – but is there a way to turn that number back into mm/dd/yyyy? Or any other format really! I’m handling the data on the front-end with Javascript, and need to be able to show the date.

This is the function that fixes the name of the column:

def fix():
    rb = xlrd.open_workbook('static/files/sheet.xls')
    wb = copy(rb)

    # open the first sheet
    w_sheet = wb.get_sheet(0)

    # row number = 0 , column number = 1
    w_sheet.write(1, 20, 'Applicant postcode')
    w_sheet.write(1, 4, 'Postcode')

    # save the file
    wb.save('static/files/fixed.xls')

    return "File has been fixed"

I’ve tried doing new Date() on those numbers, but it doesn’t work.

Any ideas?

Asked By: Gordon Maloney

||

Answers:

the number 44992 that you see is an epoch or UNIX time or in Excel, it’s a serial number.
In order to save it in the right format you need to store the dates as a string or use libraries that will do it for you and change only what you need.
Or in JS you need to convert from timestamp to Date format.

Answered By: RAI
Categories: questions Tags: ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.