JS 封装时间格式化

1. 格式化时间 为 自定义 yyyy-MM-dd hh:mm:ss

export function parseTime(time, cFormat) {
  if (arguments.length === 0) {
    return null
  }
  const format = cFormat || '{y}-{m}-{d} {h}:{i}:{s}'
  let date
  if (typeof time === 'object') {
    date = time
  } else {
    if ((typeof time === 'string') && (/^[0-9]+$/.test(time))) {
      time = parseInt(time)
    }
    if ((typeof time === 'number') && (time.toString().length === 10)) {
      time = time * 1000
    }
    date = new Date(time)
  }
  const formatObj = {
    y: date.getFullYear(),
    m: date.getMonth() + 1,
    d: date.getDate(),
    h: date.getHours(),
    i: date.getMinutes(),
    s: date.getSeconds(),
    a: date.getDay()
  }
  const time_str = format.replace(/{(y|m|d|h|i|s|a)+}/g, (result, key) => {
    let value = formatObj[key]
    // Note: getDay() returns 0 on Sunday
    if (key === 'a') { return ['日', '一', '二', '三', '四', '五', '六'][value ] }
    if (result.length > 0 && value < 10) {
      value = '0' + value
    }
    return value || 0
  })
  return time_str
}
//哪里需要使用,哪里调用
parseTime(数据,格式)
parseTime(128374926313, "{y}-{m}-{d}");

2.将时间转化为“刚刚,几分钟前,几小时前,具体日期”

如: “刚刚” “两分钟之前” “2小时前” “两天前”

export function formatTime (time: any, option: any) {
  if (('' + time).length === 10) {
    time = parseInt(time) * 1000
  } else {
    time = +time
  }
  const d:any = new Date(time)
  const now = Date.now()

  const diff = (now - d) / 1000

  if (diff < 30) {
    return '刚刚'
  } else if (diff < 3600) {
    // less 1 hour
    return Math.ceil(diff / 60) + '分钟前'
  } else if (diff < 3600 * 24) {
    return Math.ceil(diff / 3600) + '小时前'
  } else if (diff < 3600 * 24 * 2) {
    return '1天前'
  }
  if (option) {
    return dateFormat(time, option)
  } else {
    return (
      d.getMonth() +
      1 +
      '月' +
      d.getDate() +
      '日' +
      d.getHours() +
      '时' +
      d.getMinutes() +
      '分'
    )
  }
}

使用举例:

  const d = new Date('2018-07-13 17:54:01') // "2018-07-13 17:54:01"
  const retrofit = 5 * 1000

    formatTime((d / 1000).toFixed(0))  // '7月13日17时54分'
    formatTime(+new Date() - 1) // '刚刚'
    formatTime(+new Date() - 60 * 2 * 1000 + retrofit) // '2分钟前'
	formatTime(+new Date() - 60 * 60 * 2 * 1000 + retrofit) // '2小时前'
    formatTime(+new Date() - 60 * 60 * 24 * 1 * 1000) // '1天前'
    formatTime(d) // '7月13日17时54分'
    formatTime(d, '{y}-{m}-{d} {h}:{i}'// '2018-07-13 17:54'
    formatTime(d, '{y}-{m}-{d}') // '2018-07-13'
    formatTime(d, '{y}/{m}/{d} {h}-{i}')// '2018/07/13 17-54'

3. 计算两个日期时间的时间差(YY-MM-DD HH:MM:SS)格式

export function getValue (aa:any, bb:any) {
  const a = new Date(aa) // 转换为中国标准时间
  const b = new Date(bb)
  const time1 = a.getTime() // 转换为时间戳
  const time2 = b.getTime()
  let runTime = (time2 - time1) / 1000 // 开始得出时间差,然后计算
  const year = Math.floor(runTime / 86400 / 365)
  runTime = runTime % (86400 * 365)
  const month = Math.floor(runTime / 86400 / 30)
  runTime = runTime % (86400 * 30)
  const day = Math.floor(runTime / 86400)
  runTime = runTime % 86400
  const hour = Math.floor(runTime / 3600)
  runTime = runTime % 3600
  const minute = Math.floor(runTime / 60)
  runTime = runTime % 60
  const second = runTime
  return year + '年,' + month + '月,' + day + '天,' + hour + '小时,' + minute + '分,' + second + '秒'
}

使用举例:

const a:any = list.nowTime // 2018-07-13 17:54:01
const b:any = parseTime(new Date(), 'yyyy-MM-dd hh:mm:ss')
getValue(a, b)
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

王小王和他的小伙伴

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值