Vue 中格式化UTC时间 将2022-09-05T09:08:03.000Z 转换成 YYYY-MM-DD HH:mm:ss

格式化UTC时间

1.引入第三方包 dayjs (一个轻量的处理时间和日期的javascript库)
(1)下载 npm install/ i dayjs --save
(2). main.js 中全局引入

import dayjs from 'dayjs
Vue.prototype.dayjs=dayjs
  1. 在使用页面中加入函数
 timeTranslate(val) {
        var utc = require('dayjs/plugin/utc')
        dayjs.extend(utc)
        return this.dayjs(val).utc().format('YYYY-MM-DD HH:mm:ss')
      },

3.展示调用函数

 <el-table-column label="创建时间" align="center" prop="startTime" width="180">
            <template slot-scope="scope">
              <span>{{timeTranslate(scope.row.startTime)}}</span>
            </template>
          </el-table-column>

4.官方文档
https://dayjs.fenxianglu.cn/category/
5.引用官方方法

var utc = require('dayjs/plugin/utc')
dayjs.extend(utc)

// 默认当地时间
dayjs().format() //2019-03-06T17:11:55+08:00

// UTC 模式
dayjs.utc().format() // 2019-03-06T09:11:55Z

// 将本地时间转换成 UTC 时间
dayjs().utc().format() // 2019-03-06T09:11:55Z 

// 在 UTC 模式下,所有的展示方法都将使用 UTC 而不是本地时区
// 所有的 get 和 set 方法也都会使用 Date#getUTC* 和 Date#setUTC* 而不是 Date#get* and Date#set*
dayjs.utc().isUTC() // true
dayjs.utc().local().format() //2019-03-06T17:11:55+08:00
dayjs.utc('2018-01-01', 'YYYY-MM-DD') // with CustomParseFormat plugin
  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值