开发中有关各类时间转化问题汇总

类型转化

  1. yyyy-MM-dd hh:mm:ss 转化为 中国标准时间
new Date('yyyy-MM-dd hh:mm:ss') ---> Fri May 26 2023 17:59:19 GMT+0800 (中国标准时间)
  1. 中国标准时间转化 yyyy-MM-dd hh:mm:ss
export const formatDate = (date, fmt) => {
  if (/(y+)/.test(fmt)) {
    fmt = fmt.replace(RegExp.$1, (`${date.getFullYear()}`).substr(4 - RegExp.$1.length))
  }
  const o = {
    'M+': date.getMonth() + 1,
    'd+': date.getDate(),
    'h+': date.getHours(),
    'm+': date.getMinutes(),
    's+': date.getSeconds(),
    S: date.getMilliseconds()
  }
调用方法: formatDate(new Date(), 'yyyy-MM-dd hh:mm:ss')
  1. yyyy-MM-dd hh:mm:ss 转为时间戳
let tempTime = '2023-05-26 18:00:21';
let timesResult = Date.parse(new Date(tempTime ));
  1. 中国标准时间转为时间戳
let tempTime = new Date();// Sat Jun 03 2023 10:14:50 GMT+0800 (中国标准时间)
const timesResult = tempTime.getTime()
  1. 时间戳转为中国标准时间
const tempTime = 1685095909000
const timesResult = new Data(time);
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值