I check moment in the Chrome Developer Tools on the moment-timezone website: https://momentjs.com/timezone/
I execute:
moment(new Date(2011, 9, 16, 12)).tz('America/Los_Angeles').toDate()
and the result is still:
Sun Oct 16 2011 12:00:00 GMT+0200 (Central European Daylight Time)
Why moment-timezone didn't apply given timezone and I get Central European Daylight Time
?
When you create
new Date(2011, 9, 16, 12)
, it gets October 16th 2011 at 12:00 in whatever the browser's timezone is. Thentz
converts this to the specified timezone, andtoDate()
converts back to the date object (using the browser's timezone).If you want to create October 16th 2011 at 12:00 in Los Angeles timezone, there's no need to use
new Date
. You can usemoment.tz
directly:The output will be:
You can also create the date passing values instead of a string (check the docs to see all options):
All the above produce the same date (October 16th 2011 at 12:00 in Los Angeles timezone).
Note that the values are zero-indexed (January is zero), while in the string, months are 1-indexed (January is
1
).Calling
toDate()
on the above will convert the date/time to your browser's default timezone as well.