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.

2 Answers 2

6

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.

Sign up to request clarification or add additional context in comments.

Comments

0

More precise solution would be below

const ExcelDateToJSDate = (date) => {
    let converted_date = new Date(Math.round((date - 25569) * 864e5));
converted_date = moment(converted_date).format('DD-MM-YY')

1 Comment

As it’s currently written, your answer is unclear. Please edit to add additional details that will help others understand how this addresses the question asked. You can find more information on how to write good answers in the help center.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.