js讀取excel中日期格式轉(zhuǎn)換問題

// excel讀取2018/01/01這種時間格式是會將它裝換成數(shù)字類似于46254.1545151415 numb是傳過來的整數(shù)數(shù)字,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
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

友情鏈接更多精彩內(nèi)容