js读取excel时间格式转换
生活随笔
收集整理的這篇文章主要介紹了
js读取excel时间格式转换
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
使用xlsx.full.min.js 獲取excel的日期數據為:37858;
拿到的整數值是日期距離1900年1月1日的天數,這時需要寫一個函數轉換:
/*** excel導入插件方法:excel時間格式化* @param {*} numb excel時間格式* @param {*} format 轉換分隔符* @returns 標準時間格式*/ export function formatExcelDate(numb, format) {// 天數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() + ''// 轉換的格式符號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)) }參數:numb是excel轉換出來的整數值,format是年月日之間分隔符號。
將excel表格中的中文替換英文
/*** excel導入插件方法:key中文轉英文* @param {*} results 需要轉換的數據* @param {*} maps 轉換數據的中英映射關系* @param {*} times 需要格式化的excel時間屬性名* @returns*/ export function transformKeys(results, maps, times = []) {const newArr = []results.forEach((item) => {// 替換為key是英文的新對象,添加進數組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 }總結
以上是生活随笔為你收集整理的js读取excel时间格式转换的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 静音去除的问题
- 下一篇: 压敏电阻GB/T 10194测试和AEC