js读取excel数据后的时间格式转换

用nodejs的“node-xlsx”库,获取excel的数据之后,里面的日期值全面变成了一个整数值。至于,怎么使用“node-xlsx”获取数据,代码:

const xlsx = require('node-xlsx')

let sheets = xlsx.parse('./excel.xlsx')

拿到的整数值是日期距离1900年1月1日的天数,这时需要写一个函数转换:

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)
}

参数:numb是excel转换出来的整数值,format是年月日之间分隔符号。

你可能感兴趣的:(js读取excel数据后的时间格式转换)