小程序時間戳轉日期

小程序時間戳轉日期,寫一個工具類,可以直接調用。
代碼:
util.js

const formatTime = date => {
  const year = date.getFullYear()
  const month = date.getMonth() + 1
  const day = date.getDate()
  const hour = date.getHours()
  const minute = date.getMinutes()
  const second = date.getSeconds()

  return [year, month, day].map(formatNumber).join('-') + ' ' + [hour, minute, second].map(formatNumber).join(':')
}

module.exports = {
  formatTime: formatTime
}

在js中的使用:
test.js

const util = require('../../utils/util.js');
Page({
  let date = new Date();
  let time = util.formatTime(date);
})
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章