2--日期 年月日

util.js

const formatTime = date => {
  // const date = new Date(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(':')
}
//年月日
const formatTimeToYmd = data => {
  const date = new Date(data) 
  const year = date.getFullYear()
  const month = date.getMonth() + 1
  const day = date.getDate()
  return [year, month, day].map(formatNumber).join('-')
}

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

module.exports = {
  formatTime: formatTime,
  formatTimeToYmd: formatTimeToYmd
}

引用:

var util = require('../../utils/util.js')

用法:

 var time = util.formatTimeToYmd(info[0].time)

猜你喜欢

转载自blog.csdn.net/xu_ze_qin/article/details/106885266
今日推荐