js日期时间格式化

const formatTimes = 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(doubleNumber).join('/') + ' ' + [hour, minute, second].map(doubleNumber).join(':')
}

const doubleNumber = num => {
  num = num.toString()
  return num[1] ? num : '0' + num
}

module.exports = {
  formatTime: formatTimes
}

猜你喜欢

转载自www.cnblogs.com/aloehui/p/9076256.html