UTC转本地时间

第一个是类的方法实现,第二个是函数方式。推荐函数方式:

class UtcToLocal {

    static format(str) {
        return str > 9 ? str : '0' + str;
    }

    setUp(utcTime) {
        this.date = new Date(utcTime);
        this.year = this.date.getFullYear();
        this.mon = UtcToLocal.format(this.date.getMonth() + 1);
        this.day = UtcToLocal.format(this.date.getDate());
        this.hour = UtcToLocal.format(this.date.getHours());
        this.min = UtcToLocal.format(this.date.getMinutes());
        this.second = UtcToLocal.format(this.date.getSeconds());
        this.splitTime();
    }

    splitTime() {
        const localTime = `${this.year}-${this.mon}-${this.day} ${this.hour}:${this.min}:${this.second}`;
        console.log(localTime)
        return localTime;
    }
}

export default UtcToLocal;
export default function utcToLocal(utcTime) {
    if (!utcTime) {
        return;
    }
    const date = new Date(utcTime);
    const year = date.getFullYear();
    const mon = format(date.getMonth() + 1);
    const day = format(date.getDate());
    const hour = format(date.getHours());
    const min = format(date.getMinutes());
    const second = format(date.getSeconds());
    
    const localTime = `${year}-${mon}-${day} ${hour}:${min}:${second}`;
    return localTime;
}

function format(str) {
    return str > 9 ? str : '0' + str;
}

第三个是使用moment.js

moment.utc(commonOne.valid_receive_end_time).local().format('YYYY-MM-DD hh:mm:ss')
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • Android 自定义View的各种姿势1 Activity的显示之ViewRootImpl详解 Activity...
    passiontim阅读 173,663评论 25 708
  • 用两张图告诉你,为什么你的 App 会卡顿? - Android - 掘金 Cover 有什么料? 从这篇文章中你...
    hw1212阅读 12,993评论 2 59
  • 小可感情出现问题之后,和我约好去散心,担心她的状态,想带她跋山涉水,希望大汗淋漓能代谢走一些东西,旅行的新鲜能带来...
    DTttt阅读 232评论 0 0