"2020-04-15T03:22:58.000000Z"时间格

2020-04-14  本文已影响0人  plum_meizi

经常会遇到后端传来的时间没有转换格式的,像"2020-04-15T03:22:58.000000Z"
可以写个函数来转换成常见的yyyy-mm-dd hh:mm:ss

gettime(time) {
    let date = new Date(time);
    let yyyy = date.getFullYear();
    let mm = this.twofix(date.getMonth() + 1);//js的月份是从0开始计算的,要加1
    let dd = this.twofix(date.getDate());
    let h = this.twofix(date.getHours());
    let m = this.twofix(date.getMinutes());
    let s = this.twofix(date.getSeconds());
    return `${yyyy}-${mm}-${dd} ${h}:${m}:${s}`;
}
//在1位数的前面补上0
twofix(s) {
    let d: any = null;
    if (s < 10) {
        d = `0${s}`;
    } else {
        d = s;
    }
    return d;
}
上一篇下一篇

猜你喜欢

热点阅读