格式化时间戳

作者: 万年对朝夕 | 来源:发表于2019-01-02 14:15 被阅读5次
DateScheme = (timestamp = new Date().getTime(), format = 'Y-M-D h:m:s') => {
  if (!/^[\d]{10,13}$/.test(timestamp)) {
    console.error('只能格式化10,13位时间戳')
    return false
  }
  const _time = timestamp.length === 10 ? (timestamp * 1000) : (timestamp * 1)
  const date = new Date(_time)
  const data = {
    Y: date.getFullYear(),
    M: date.getMonth() + 1,
    D: date.getDate(),
    h: date.getHours(),
    m: date.getMinutes(),
    s: date.getSeconds()
  }
  return format.replace(/[YMDhms]/g, function (m) {
    return data[m] < 10 ? ('0' + data[m]) : data[m]
  })
}

相关文章

网友评论

    本文标题:格式化时间戳

    本文链接:https://www.haomeiwen.com/subject/yvjzlqtx.html