momentjs toDate() - timezone gets reset

后端 未结 3 552
长发绾君心
长发绾君心 2021-02-13 22:12

I am working with momentjs and converting dates to different time zones using convertedDate = moment().utcOffset(timezone).format(). This works well but it is a str

3条回答
  •  执念已碎
    2021-02-13 22:17

    This should work:

    I have the same issue. Just get the Date as a string using the same approach that you are using. Let's say your date is, for example: '2018-08-05T10:00:00'.

    Now you need the Date object with correct time. To convert String into object without messing around with timezones, Use getTimezoneOffset:

    var date = new Date('2016-08-25T00:00:00')
    var userTimezoneOffset = date.getTimezoneOffset() * 60000;
    new Date(date.getTime() - userTimezoneOffset);
    

    getTimezoneOffset() will return either negative or positive value. This must be subtracted to work in every location in the world.

提交回复
热议问题