js读取excel时间格式转换
生活随笔
收集整理的這篇文章主要介紹了
js读取excel时间格式转换
小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.
使用xlsx.full.min.js 獲取excel的日期數(shù)據(jù)為:37858;
拿到的整數(shù)值是日期距離1900年1月1日的天數(shù),這時需要寫一個函數(shù)轉(zhuǎn)換:
/*** excel導(dǎo)入插件方法:excel時間格式化* @param {*} numb excel時間格式* @param {*} format 轉(zhuǎn)換分隔符* @returns 標(biāo)準時間格式*/ export function formatExcelDate(numb, format) {// 天數(shù)const time = new Date((numb - 1) * 24 * 3600000 + 1)console.log(time)time.setYear(time.getFullYear() - 70)const year = time.getFullYear() + ''const month = time.getMonth() + 1 + ''const date = time.getDate() + ''// 轉(zhuǎn)換的格式符號if (format && format.length === 1) {return new Date(year + format + month + format + date)}return (year + (month < 10 ? '0' + month : month) + (date < 10 ? '0' + date : date)) }參數(shù):numb是excel轉(zhuǎn)換出來的整數(shù)值,format是年月日之間分隔符號。
將excel表格中的中文替換英文
/*** excel導(dǎo)入插件方法:key中文轉(zhuǎn)英文* @param {*} results 需要轉(zhuǎn)換的數(shù)據(jù)* @param {*} maps 轉(zhuǎn)換數(shù)據(jù)的中英映射關(guān)系* @param {*} times 需要格式化的excel時間屬性名* @returns*/ export function transformKeys(results, maps, times = []) {const newArr = []results.forEach((item) => {// 替換為key是英文的新對象,添加進數(shù)組const _item = {}for (const key in item) {const enKey = maps[key]if (times.includes(enKey)) {// 格式化excel時間_item[enKey] = formatExcelDate(item[key], '-')} else {_item[enKey] = item[key]}}newArr.push(_item)})return newArr }總結(jié)
以上是生活随笔為你收集整理的js读取excel时间格式转换的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 静音去除的问题
- 下一篇: 压敏电阻GB/T 10194测试和AEC