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. i want to convert the UTC time to the local Users time zone.
var timezone = "America/New_York";
var utcDate = "2013-10-16T10:31:59.0537721Z";
var localDate = moment(utcDate).utc().tz(timezone).format()
When i try to do this i'm getting wrong time. not sure how to get this working with moment
for anyone having the same problem:
your date iso format should contain the timezone. here is the format allowed by momentjs:
notice the +00:00 mean it is UTC
example
will terturn
Even if you don't know the timezone, you get the client offset and use like this,
Try this JsFiddle
Use the
+
operator to get unix time, then: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 obejctmoment(Number)
creates a new moment object in the user's local time zone, using the passed in unix offset.You can try:
But it shouldn't matter. They should both produce:
"2013-10-16T06:31:59-04:00"
.It works for me, running on Chrome 30, so it's probably browser related.
If you're running Moment.js 2.3.1 or earlier on IE8, then it's a side effect of issue #1175, which was fixed in 2.4.0. Updating to the latest version should solve the problem.