前端 xlsx js javascript 处理excel 数据展示 日期格式处理
1、
参考
https://blog.csdn.net/Seven71111/article/details/107375712
https://blog.csdn.net/weixin_44987713/article/details/130129282
https://blog.csdn.net/qq_57952018/article/details/134812452
2、
存在的问题
a、
千年虫
b、
定义一个转换日期格式的方法(转成日期格式:YYYY-MM-DD)
注意的点:xlsx将excel中的时间内容解析后,会小一天
如2020/11/3,xlsx会解析成 Mon Nov 02 2020 23:59:17 GMT+0800 小了43秒
当再用moment转换成日期时:
Mon Nov 02 2020 23:59:17 GMT+0800 会转成2020/11/2 所以需要在moment转换后+1天 november 十一月
————————————————
版权声明:本文为博主原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。
原文链接:https://blog.csdn.net/Seven71111/article/details/107375712
3、
// https://blog.csdn.net/Seven71111/article/details/107375712 export const convertExcelDate = (date: any) => { /*日期格式 有 2024.03.01 2024-03-01 2024/03/01 2024-1-1 等 可以被excel插件识别 */ // if(/^\d+$/.test(date)){ // // 纯数字 // } // if( !(/^(19|20)\d\d\/(0[1-9]|1[0-2])\/(0[1-9]|[12][0-9]|3[01])$/.test(date))){ // return date // } if (date === undefined || date === null || date === '') { return '' } if( typeof date !== 'object'){ return date } const formatDateStr = moment(date).format('YYYY/MM/DD') if (formatDateStr === 'Invalid date') { return date } return moment(date).add(1, 'days').format('YYYY/MM/DD') }
export const formatDateStr2 = (numb: any, format:any) => {
console.log(numb + '---------' + format);
if (numb.toString().indexOf('/') != -1) return numb
const time = new Date((numb - 1) * 24 * 3600000 + 1)
time.setYear(time.getFullYear() - 70)
const year = time.getFullYear() + ''
const month = time.getMonth() + 1 + ''
const date = time.getDate() + ''
if (leapyear(year)) {
date = time.getDate() - 1 + ''
}
if (format && format.length === 1) {
return month + format + date
}
// (month < 10 ? '0' + month : month)+(date < 10 ? '0' + date : date)
return month + '/' + date //需要的格式
}
//区分闰年与平年 最开始没区分 出现了4/0这种情况 日期少了一天
const leapyear = (year: any) => {
var flag = false;
if ((year % 4 == 0 && year % 100 != 0) || year % 400 == 0) {
flag = true;
}
return flag;
}