• 首页 首页 icon
  • 工具库 工具库 icon
    • IP查询 IP查询 icon
  • 内容库 内容库 icon
    • 快讯库 快讯库 icon
    • 精品库 精品库 icon
    • 问答库 问答库 icon
  • 更多 更多 icon
    • 服务条款 服务条款 icon

JS去掉Json时间日期格式带T的日期

武飞扬头像
我总是词不达意
帮助1

在项目中有遇到对于后台传过来的json数据时间格式是带T的一些做法总结

方式一 

  1.  
    //时间格式中含有T
  2.  
    let date = '2022-12-15T16:14:24',
  3.  
    dateTimeres = date.replace(/T/g, ' ').replace(/.[\d]{3}Z/, ' ')
  4.  
    console.log(dateTimeres, 'dateTimeres')

 方式二

  1.  
    let date = '2022-12-15T16:14:24',
  2.  
    dateTimeres = new Date( new Date(date) 8 * 3600 * 1000)
  3.  
    .toISOString()
  4.  
    .replace(/T/g, ' ')
  5.  
    .replace(/\.[\d]{3}Z/, '')
  6.  
    console.log(dateTimeres, 'dateTimeres')

 方式三 (最准确,但繁琐)

  1.  
    //去除Json日期格式的T
  2.  
    function TimeFormat(time) {
  3.  
    let d = time ? new Date(time) : new Date(),
  4.  
    obj = {
  5.  
    year: d.getFullYear(),
  6.  
    month: d.getMonth() 1,
  7.  
    day: d.getDate(),
  8.  
    hours: d.getHours(),
  9.  
    min: d.getMinutes(),
  10.  
    seconds: d.getSeconds()
  11.  
    }
  12.  
    Object.keys(obj).forEach(key => {
  13.  
    if (obj[key] < 10) obj[key] = `0${obj[key]}`
  14.  
    // console.log(obj[key])
  15.  
    })
  16.  
     
  17.  
    return `${obj.year}-${obj.month}-${obj.day} ${obj.hours}:${obj.min}:${obj.seconds}`
  18.  
    }
学新通
  1.  
    //时间格式中含有T
  2.  
    let date = '2022-12-15T16:14:24'
  3.  
    console.log(TimeFormat(date), 'dateTimeres')

这篇好文章是转载于:学新通技术网

  • 版权申明: 本站部分内容来自互联网,仅供学习及演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,请提供相关证据及您的身份证明,我们将在收到邮件后48小时内删除。
  • 本站站名: 学新通技术网
  • 本文地址: /boutique/detail/tanhgbhbba
系列文章
更多 icon
同类精品
更多 icon
继续加载