73

I am using Moment.js to handle dates in my web application. The server returns all the dates in milliseconds UTC. Now, I have to display the dates applying a specific timezone (based on the user settings).

Is there any way to set the timezone globally instead of changing all the calls to momentjs to handle it?

Alexander Abakumov
  • 13,617
  • 16
  • 88
  • 129
Fabrizio Fortino
  • 1,479
  • 1
  • 14
  • 22
  • Do you mean the user's local time zone of their computer? Or do you mean a time zone string from your own code, such as `America/New_York`? – Matt Johnson-Pint Jul 17 '13 at 19:56
  • I mean a time zone string coming from the server, such America/New_York. I know I can handle it with momentjs-timezone. My question is: how can I set it once when my application starts instead of modifying all the calls to moment() with moment().tz('America/New_York') ? – Fabrizio Fortino Jul 18 '13 at 07:43
  • Sorry, that feature doesn't exist. You can request it in the [moment-timezone issue tracker](https://github.com/moment/moment-timezone/issues). Thanks. – Matt Johnson-Pint Jul 18 '13 at 17:53
  • Thanks Matt. I have just opened an [issue](https://github.com/moment/moment-timezone/issues/15). – Fabrizio Fortino Jul 19 '13 at 08:35

5 Answers5

108

You can set the default timezone in Moment by using:

moment.tz.setDefault(String);

For example

moment.tz.setDefault("America/New_York");
Oliver Salzburg
  • 21,652
  • 20
  • 93
  • 138
  • 13
    Works well, just remember to add `moment-timezone` from npm and `import moment from "moment-timezone";` instead of importing `moment` from `moment`. – Lauri Harpf Sep 06 '19 at 14:06
10
  1. npm install moment-timezone
  2. var moment = require('moment-timezone'); and use this object instead of usual moment.
  3. moment.tz.setDefault(String); where String is a time zone identifier.

For example:

var moment = require('moment-timezone');
moment.tz.setDefault("America/New_York");

Docs: https://momentjs.com/timezone/docs/

3

Use the moment-timezone library found on the same website: http://momentjs.com/timezone/ It let's you do things like:

moment(utcDateTime).tz(settings.timezone).format(settings.dateFormat);

I recommend implementing a user class/object that has a service function for translating UTC to the user's timezone. Have a look at this fiddle:

http://jsfiddle.net/guidosch/ofd4unhu/4/

The dates are served as UTC, but the view uses the date formatting method of the user class to adjust and format the UTC date according to the user's preferences.

guidos
  • 522
  • 6
  • 11
-6

Having run into this problem in the past, my solution was to create a moment factory that provisioned moment objects rom a base configuration. You can make it as transparent as requiring moment - referencing your package and using the class just like moment - but in reality you are calling a moment wrapper object that provisions the moment implementations with the selected timeZone.

Scott Fanetti
  • 126
  • 1
  • 5
-13

I've not done extensive testing, but it looks right on cursory tests. I was able to do this with Moment Timezone 0.0.1:

var serverTimezoneOffset = <?php echo timezone_offset_get(new DateTimeZone(date_default_timezone_get()), new DateTime('now')) / -60; ?>;
moment.updateOffset(new Date().getTimezoneOffset()-serverTimezoneOffset);
stephenr85
  • 151
  • 1
  • 4