momentjs toDate() - timezone gets reset

后端 未结 3 547
长发绾君心
长发绾君心 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:33

    A cleaner approach to get a native Date object with time according to the timezone, using moment would be following:

    convertedDate = moment.utc(moment.tz(timezone).format('YYYY-MM-DDTHH:mm:ss')).toDate()
    

    PS: assuming two things

    • you have imported both 'moment' and 'moment-timezone'.
    • value of timezone is given like 'Asia/Kolkata' instead of an offset value

提交回复
热议问题