Time format conversion after JS reads excel data

1. Convert the exceldate value to the normal date format

  • The date data xlsx.full.min.jsobtained using is:excel42358

    function formatDate (numb, format) {
    let time = new Date((numb - 1) * 24 * 3600000 + 1)
    time.setYear(time.getFullYear() - 70)
    let year = time.getFullYear() + ''
    let month = time.getMonth() + 1 + ''
    let date = time.getDate() + ''
    if(format && format.length === 1) {
    return year + format + month + format + date
    }
    return year+(month < 10 ? '0' + month : month)+(date < 10 ? '0' + date : date)
    }

    console.log(formatDate(42358, '/'))</p> <p>Output: <code>2015/12/20</code>

2. Convert the normal date format to a Exceldate value

  • Calculate 1900-01-012015-12-20how many days apart

    function fun () {
    let startTime = new Date ( '1900-01-01' ); // start time
    let endTime = new Date ( '2015-12-20' ); // end time
    // interval days, why do you need +2, What is calculated here is only the difference days in the middle, plus the beginning and the end of each day is 2 days, so +2
    return Math .floor((endTime - startTime) / 1000 / 60 / 60 / 24 ) + 2
    }

    console.log(fun())</p> <p>Output: <code>42358</code>

Leave a Comment

Your email address will not be published. Required fields are marked *