How to create time in a specific time zone with moment.js

前端 未结 4 1720
無奈伤痛
無奈伤痛 2020-12-06 04:09

I have this backend that sends me a pre formatted time in a set time zone, but without any information for the said time zone. The strings are like: \"2013-08-26 16:55:00\".

4条回答
  •  予麋鹿
    予麋鹿 (楼主)
    2020-12-06 04:46

    I had a similar issue for which i had to use New York based time so i had to consider for daylight savings. I tried using the above few answers but wasn't able to get it working. Then I solved my issue like the below code

    import moment from 'moment-timezone'
    
    const time = timestamp
    const offset = moment.tz.zone('America/New_York')?.parse(time)
    const date = moment(time).add(offset, 'minutes').toISOString()
    

    this gives you an ISO string which you can use as below

    moment(date).local().format('ffffdd, MMM DD YYYY, hh:mm A')

    or in whatever format you would like to display it

提交回复
热议问题