用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是年月日之间分隔符号。
前端李大人 2019-01-10 10:01:41 9676 收藏 3
文章标签: nodejs excel时间格式 nodejs读取excel js excel时间转换
版权声明:本文为博主原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。
本文链接:https://blog.csdn.net/u014789022/article/details/86215514