vue前端对时间的处理

目录

1.中国标准时间转换为标准年月日时分秒

2.中国标准时间转换为年月日当天的凌晨00:00:00

3.中国标准时间转换为年月日当天的23:59:59

4.中国标准时间转换为年月日时分秒

5.时间范围内筛选数据(以本周为例)

6.中国标准时间转换成时间戳

7.时间戳转化成中国标准时间


1.中国标准时间转换为标准年月日时分秒

// 中国标准时间格式化
    formatDate(date, fmt) {
      if (!date) {
        return ''
      }
      if (/(y+)/.test(fmt)) {
        fmt = fmt.replace(
          RegExp.$1,
          (date.getFullYear() + '').substr(4 - RegExp.$1.length)
        )
      }
      let o = {
        'M+': date.getMonth() + 1,
        'd+': date.getDate(),
        'h+': date.getHours(),
        'm+': date.getMinutes(),
        's+': date.getSeconds()
      }
      for (let k in o) {
        if (new RegExp('(' + k + ')').test(fmt))
          fmt = fmt.replace(
            RegExp.$1,
            RegExp.$1.length === 1
              ? o[k]
              : ('00' + o[k]).substr(('' + o[k]).length)
          )
      }
      return fmt
    }

2.中国标准时间转换为年月日当天的凌晨00:00:00

const time = new Date();

console.log(time)//输出结果 : Wed Jan 12 2022 11:01:17 GMT+0800 (中国标准时间)

const end = formatDate(new Date(), "yyyy-MM-dd 00:00:00");

console.log(end)//输出结果  : 2022-01-12 00:00:00

3.中国标准时间转换为年月日当天的23:59:59

const time = new Date();

console.log(time)//输出结果 : Wed Jan 12 2022 11:01:17 GMT+0800 (中国标准时间)

const end = formatDate(new Date(), "yyyy-MM-dd 23:59:59");

console.log(end)//输出结果  : 2022-01-12 23:59:59

4.中国标准时间转换为年月日时分秒

const time = new Date();

console.log(time)//输出结果 : Wed Jan 12 2022 11:01:17 GMT+0800 (中国标准时间)

const end = formatDate(new Date(), "yyyy-MM-dd hh:mm:ss");

console.log(end)//输出结果  : 2022-01-12 11:01:17

5.时间范围内筛选数据(以本周为例)

end = new Date()
end.setHours(0, 0, 0, 0) //设置筛选结束时间为(0, 0, 0, 0)
start = new Date()
start.setTime(start.getTime() - 86400000 * 7)
this.timeRange = [start, end]

6.中国标准时间转换成时间戳

new Date().getTime()

new Date()           // Wed Jan 12 2022 11:14:50 GMT+0800 (中国标准时间)
new Date().getTime() // 1641957294080

7.时间戳转化成中国标准时间

new Date(start)

举例,获取当前中国标准时间 Wed Jan 12 2022 11:17:32 GMT+0800 (中国标准时间),取上个月这一天的时间。那么就需要先转成时间戳,再减去一个月的时间,再转成中国标准时间就可以了。

let start = new Date().getTime() - 3600 * 1000 * 24 * 30;
console.log(start) //1639365452201
new Date(start);
console.log(start)//Mon Dec 13 2021 11:17:32 GMT+0800 (中国标准时间)

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值