0

I am having a weird issue when I was reading dates from excel files in a react application. When I read it, it comes out as some form of float but what's weird is that it only does this for 1th to 12th date of the month from 13th till 31th is fine.

What I mean is that a date like 01-01-81 gets converted to 29587.00013888889 but 13-01-81 remains in its original.

I found this one solution from here How to read date from an excel file in JavaScript. But it does not give back the original value.

Really appreciate any kind of help. Cheers.

ahsan mukhtar
  • 423
  • 7
  • 21

1 Answers1

4

The problem was that excel has its own way for dates and we need to convert it, into the format we need. This is the method I used

const ExcelDateToJSDate = (date) => {
        let converted_date = new Date(Math.round((date - 25569) * 864e5));
        converted_date = String(converted_date).slice(4, 15)
        date = converted_date.split(" ")
        let day = date[1];
        let month = date[0];
        month = "JanFebMarAprMayJunJulAugSepOctNovDec".indexOf(month) / 3 + 1
        if (month.toString().length <= 1)
            month = '0' + month
        let year = date[2];
        return String(day + '-' + month + '-' + year.slice(2, 4))
    }

You can simply pass the excel date and change the function according to the date format you need.

ahsan mukhtar
  • 423
  • 7
  • 21