vue 时间格式相互转化以及判断汇总

12 篇文章 0 订阅
8 篇文章 0 订阅

vue 时间格式相互转化

1、转化YYYYMMDDHHMMSS时间格式为YYYY-MM-DD HH:MM:ss

export function changeTime(time) {
  var newtime = ''
  newtime = time.substring(0, 4) + '-' + time.substring(4, 6) + '-' + time.substring(6, 8) + ' ' + time.substring(8, 10) + ':' + time.substring(10, 12) + ':' + time.substring(12, 14)
  return newtime
}

2、转化时间戳格式为 YYYY-MM-DD HH:MM:ss

export function timestampToTime(timestamp) {
  var date = new Date(timestamp)
  var Y = date.getFullYear() + '-'
  var M = (date.getMonth() + 1 < 10 ? '0' + (date.getMonth() + 1) : date.getMonth() + 1) + '-'
  var D = (date.getDate() < 10 ? '0' + date.getDate() : date.getDate()) + ' '
  var h = (date.getHours() < 10 ? '0' + date.getHours() : date.getHours()) + ':'
  var m = (date.getMinutes() < 10 ? '0' + date.getMinutes() : date.getMinutes()) + ':'
  var s = (date.getSeconds() < 10 ? '0' + date.getSeconds() : date.getSeconds())
  return Y + M + D + h + m + s
}

3、将类似格式2020-02-11T12:24:18.000+0000 转化为 YYYY-MM-DD HH:MM:ss

export function renderTime(date) {
  var dateee = new Date(date).toJSON();
  return new Date(+new Date(dateee) + 8 * 3600 * 1000).toISOString().replace(/T/g, ' ').replace(/\.[\d]{3}Z/, '')
}

4、通过时间戳判断是否过期 若过期返回1 未过期返回0

export function TimeIsPassed(time) {
  var currentTime = Date.parse(new Date())
  if(time >= currentTime){
    return 0
  }
  else{
    return 1
  }
}

5、通过时间戳获取距当前时间之间相隔的天数

export function TimestampBetweenDays(data){
  var now = new Date().getTime()
  return Math.floor((data - now) / (24*60*60*1000))
}

6、毫秒转换为天数

export function MicrosecondChangeDays(data) {
  return Math.floor(data / 86400000)
}
  • 1
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值