I use the Moment.js and Moment-Timezone frameworks, and have a Moment.js date object which is explicitly in UTC timezone. How can I convert that to the current timezone of the browser?
var testDateUtc = moment.tz("2015-01-30 10:00:00", "UTC");
var localDate = ???
So it would be fine if I could find out the users local time zone; or alternatively I'd like to convert the date object into another data object which just uses the "local timezone", no matter what that actually is.
See: http://momentjs.com/docs/#/manipulating/local/
You do not need to use moment-timezone for this. The main moment.js library has full functionality for working with UTC and the local time zone.
From there you can use any of the functions you might expect:
Note that by passing
testDateUtc
, which is amoment
object, back into themoment()
constructor, it creates a clone. Otherwise, when you called.local()
, it would also change thetestDateUtc
value, instead of just thelocalDate
value. Moments are mutable.Also note that if your original input contains a time zone offset such as
+00:00
orZ
, then you can just parse it directly withmoment
. You don't need to use.utc
or.local
. For example:Here's what I did:
Where
{{ time }}
is the utc timestamp.Use utcOffset function.