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

后端 未结 4 1301
故里飘歌
故里飘歌 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:09

    Here's what I did:

    var timestamp = moment.unix({{ time }});
    var utcOffset = moment().utcOffset();
    var local_time = timestamp.add(utcOffset, "minutes");
    var dateString = local_time.fromNow();
    

    Where {{ time }} is the utc timestamp.

提交回复
热议问题