// excel读取2018/01/01这种时间格式是会将它装换成数字类似于46254.1545151415 numb是传过来的整数数字,format是之间间隔的符号
function formatDate(numb) {
if (numb > 0) { // 先解析时间
const time = new Date((numb - 1) * 24 * 3600000 + 1);
let h = time.getHours() + 16;
let yeraData = new Date(1900, 0, numb - 1)
let year = yeraData.getFullYear();
let month = yeraData.getMonth() + 1
month = month < 10 ? '0' + month : month;
let day = yeraData.getDate()
day = day < 10 ? '0' + day : day;
if (h > 23) {
h = h - 24;
}
let m = time.getMinutes() < 10 ? "0" + time.getMinutes() : time.getMinutes();
let s = time.getSeconds() < 10 ? "0" + time.getSeconds() : time.getSeconds();
return `${year}/${month}/${day}`;
} else {
// console.log(numb)
return '非法日期格式';
}
}
export {
formatDate
}
// console.log(formatDate(42618, '/')) // 2016-9-5
或者
formatDate(numb, format) {
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() - 1 + ''
if (format && format.length === 1) {
return year + format + month + format + date
}
return year + (month < 10 ? '0' + month : month) + (date < 10 ? '0' + date : date)
},
console.log(formatDate(42618, '/')) // 2016-9-5