Create a local date from a UTC date string in Moment.js

前端 未结 2 1995
野性不改
野性不改 2021-01-26 18:23

Given a UTC date string (formatted: YYYYMMDDHHmmss) I\'d like to create a date with the local timezone using Moment.js. I have tried the following:

var utcDateSt         


        
2条回答
  •  迷失自我
    2021-01-26 18:48

    // UTC time
    var utcDateStr = '20140101120000';
    
    // First way
    var offset = moment().utcOffset();
    var localtime = moment.utc(utcDateStr,'YYYYMMDDHHmmss').utcOffset(offset);
    
    // Another way
    var anotherLocaltime= moment.utc(utcDateStr, 'YYYYMMDDHHmmss').local();
    

    Both the way works just fine... See the JsFiddle

提交回复
热议问题