excel时间处理函数

一般我们从excel中得到的数据中的时间一般是 数字,因为excel表格内部做了转换

但是我们需要把它转化为 2022/12/31 这种格式的话该怎么做呢,这时候我们就需要用到这个方法了

// 把excel文件中的日期格式的内容转回成标准时间
export function formatExcelDate(numb, format = '/') {
  const time = new Date((numb - 25567) * 24 * 3600000 - 5 * 60 * 1000 - 43 * 1000 - 24 * 3600000 - 8 * 3600000)
  time.setYear(time.getFullYear())
  const year = time.getFullYear() + ''
  const month = time.getMonth() + 1 + ''
  const 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)
}

我们只需要调用这个方法,就可以将日期转化为我们想要的格式.2022/12/31

如果想要转换为更多的格式还可以再使用Moment.js 中文网进行处理


 

猜你喜欢

转载自blog.csdn.net/qq_52006046/article/details/128525685