Changing the Utc Date to Local Date using moment.js

后端 未结 4 1945
忘掉有多难
忘掉有多难 2020-12-17 02:01

i\'m using moment.js with timezones for date manipulation in a webapp. i\'m storing all the dates in UTC and return them to UI as UTC. I\'m having the timezone of the user.

4条回答
  •  误落风尘
    2020-12-17 03:01

    Use the + operator to get unix time, then:

    moment(+moment.utc(utcDate))
    

    How it works:

    • moment.utc(String) parses the string and returns a moment object set to UTC timezone.
    • + returns the unix offset in milliseconds for the moment obejct
    • moment(Number) creates a new moment object in the user's local time zone, using the passed in unix offset.

提交回复
热议问题