有没有工具包?提供字符串或对象是否为空,和日期格式化等方法的工具包?

发布于 7 年前作者 nsun11994 次浏览最后编辑 7 年前来自 ask

有没有工具包?提供字符串或对象是否为空,和日期格式化等方法的工具包?

2 回复
ming28
ming281 楼6 年前

自己写一个模块就行了,不是很复杂的js

syan
syan2 楼5 年前

日期格式化的函数在默认模板里就有

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(':')
}

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

module.exports = {
  formatTime: formatTime
}