admin管理员组

文章数量:1401657

Is there a way to set the timezone using moment.js or moment-timezone always to reflect 'America/New_York' regardless of where the user is accessing the app from?

I've tried this but the object's timezone is reflective of the browser's timezone and returns America/Los_Angeles.

moment.tz("8/26/2013 4:55 pm", "M/D/YYYY h:mm a", "America/New_York")

Is there a way to set the timezone using moment.js or moment-timezone always to reflect 'America/New_York' regardless of where the user is accessing the app from?

I've tried this but the object's timezone is reflective of the browser's timezone and returns America/Los_Angeles.

moment.tz("8/26/2013 4:55 pm", "M/D/YYYY h:mm a", "America/New_York")
Share Improve this question asked Apr 8, 2016 at 18:52 anmanm 1,9273 gold badges19 silver badges30 bronze badges 2
  • Your syntax is totally fine here. When I run it, it produces a date with a -4 offset which is correct. When you say 'the object's timezone', what property are you looking at? – Maggie Pint Commented Apr 8, 2016 at 20:27
  • 1 Check the debug console. Are you getting an error "Moment Timezone has no data for America/New_York"? If so, you don't have the tz data loaded. See here. – Matt Johnson-Pint Commented Apr 8, 2016 at 22:58
Add a ment  | 

2 Answers 2

Reset to default 5

As stated here,

Moment.js docs

you need to include the timezone module and use it like this:

moment("8/26/2013 4:55 pm", "M/D/YYYY h:mm a").tz("America/New_York").format();

To change the default time zone, use moment.tz.setDefault with a valid time zone:

moment.tz.setDefault("America/New_York");

moment.js documentation

本文标签: