Encountered a demand:
After selecting the time, it will be printed out in the format "2020-09-29T18:02:02.000Z", and the background needs to be converted to the format "2020-09-30 02:02:02".
1. 2020-09-29T18:02:02.000Z
T represents the separator, and Z represents UTC.
UTC: Universal standard time, adding 8 hours to the universal standard time, which is the Eastern Eighth District time, which is Beijing time.
2. 2020-09-29T18:02:02.000Z is converted to 2020-09-30 02:02:02 Steps:
①. Introduce dayjs (a lightweight javascript library for processing time and date)
- Download npm install dayjs --save
Import dayjs from'dayjs' globally introduced in main.js
Vue.prototype.dayjs = dayjs;
②. Time conversion function
// 时间
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 front-end communication QQ group: 327814892
Today's horoscope of the twelve constellations, scan the code on QQ to check the horoscope and receive cash red envelopes