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

前端 未结 2 1994
野性不改
野性不改 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

    0 讨论(0)
  • 2021-01-26 18:55

    Just use the .local() function, added in version 1.5.0.

    var localDate = moment.utc(utcDateStr, 'YYYYMMDDHHmmss').local();
    
    0 讨论(0)
提交回复
热议问题