遇到一个需求:
javascript
选择时间后打印出来是 “2020-09-29T18:02:02.000Z” 格式的,后台须要转换为 “2020-09-30 02:02:02”格式。前端
1、2020-09-29T18:02:02.000Zjava
T表示分隔符,Z表示的是UTC。web
UTC:世界标准时间,在世界标准时间上加上8小时,即东八区时间,也就是北京时间。npm
2、 2020-09-29T18:02:02.000Z转换为 2020-09-30 02:02:02 步骤:函数
①.引入 dayjs (一个轻量的处理时间和日期的javascript库)this
- 下载 npm install dayjs --save
- main.js 中 全局引入
import dayjs from ‘dayjs’
Vue.prototype.dayjs = dayjs;
②.时间转换函数spa
// 时间 aaa() { let time = '2020-09-29T18:02:02.000Z' time = this.formateDate(time); console.log(form); // 2020-09-30 2:2:2 }, // 时间格式转换 formateDate(time) { // 使用dayjs 把世界标准时间转换为北京时间 let date = this.dayjs(time).format(); console.log(date) // 2020-09-30T02:02:02+08:00 // 把2020-09-30T02:02:02+08:00 截取出 '2020-9-30 2:2:2' const arr = date.split("T"); const d = arr[0]; const darr = d.split("-"); const t = arr[1]; const tarr = t.split(".000"); const marr = tarr[0].split(":"); const dd = parseInt(darr[0]) + "-" + parseInt(darr[1]) + "-" + parseInt(darr[2]) + " " + parseInt(marr[0]) + ":" + parseInt(marr[1]) + ":" + parseInt(marr[2]); console.log(dd) // 2020-9-30 2:2:2 return dd; },
web前端交流QQ群:327814892prototype
十二星座的今日运势,QQ扫码查看星座运势,还能领取现金红包
code