How to convert Moment.js date to users local timezone?

后端 未结 4 1303
故里飘歌
故里飘歌 2020-12-01 02:17

I use the Moment.js and Moment-Timezone frameworks, and have a Moment.js date object which is explicitly in UTC timezone. How can I convert that to the current timezone of t

4条回答
  •  情歌与酒
    2020-12-01 03:00

    var dateFormat = 'YYYY-DD-MM HH:mm:ss';
    var testDateUtc = moment.utc('2015-01-30 10:00:00');
    var localDate = testDateUtc.local();
    console.log(localDate.format(dateFormat)); // 2015-30-01 02:00:00
    
    1. Define your date format.
    2. Create a moment object and set the UTC flag to true on the object.
    3. Create a localized moment object converted from the original moment object.
    4. Return a formatted string from the localized moment object.

    See: http://momentjs.com/docs/#/manipulating/local/

提交回复
热议问题