We're trying to find a problem with tests that use moment.js and fail when run on a server in Arizona but succeed when run locally here in the UK. We manually set the locale before creating a moment and can see that the local 'en-gb' is installed.
This fiddle highlights what I think the problem is (Need to set computer to Arizona Time zone first!) https://jsfiddle.net/2ve10ax4/
moment.locale('en-gb');
console.log(moment.locale());
// en-gb
console.log(moment('2016-05-01T00:00:00+00:00').format());
// 2016-04-30T17:00:00-07:00
I'm expecting to see the date formatted with respect to 'en-gb' locale but it shows it with respect to Arizona time. Am I missing something?
Locale and timezone are orthogonal things. A locale does not specify a timezone unambigously. You need to set your timezone separately. You can use Momemt Timezone, and then you can use e.g.
.tz('Europe/London')
By setting moment.locale('en-gb') you have specified a locale/language (brittish-english), not a timezone.
To specify a timezone you need to include the Moment.js Timezone library in your project, and then do something like:
console.log(moment('2016-05-01T00:00:00+00:00').tz('America/Phoenix').format());
The above will give you the timezone for Arizona.
After reading more I believe parseZone is the correct solution, given that I am already including Time Zone information in the date string.
console.log(moment.parseZone('2016-05-01T00:00:00+00:00').format());
Related
So when we configure a default timezone using moment-timezone we do something like this:
moment.tz.setDefault('America/Sao_Paulo')
I was looking for a way to retrieve that value (America/Sao_Paulo) from the moment.tz object... I wasn't able to find it on their documentation page :/ I was assuming it would be something similar to moment.tz.getCurrent() // America/Sao_Paulo
Btw, this is not the same as moment.tz.guess(), because I don't want the user's local timezone, I want literally what is the timezone configured currently on moment, regardless of the browser's timezone.
I'm also in a similar boat. There appears to be an undocumented attribute on moment that is set after you guess the timezone.
moment.defaultZone
Haven't found how it can be used with other moment-timezone methods yet.
Date received from server is in UTC timezone and I need to convert it to a particular timezone, for example : America/New_York .Following is the code for same
<span class="bold" ng-bind="sess.date_time | amTimezone:'America/New_York' | amDateFormat:'h:mm a'"></span>
But on doing so I get the following error:
Moment Timezone has no data for America/New_York. See http://momentjs.com/timezone/docs/#/data-loading/.
But America/New_York is a known timezone to moment but still it is asking me to add the Timezone.
You need to load all of the following:
moment
moment-timezone
time zone data for moment-timezone
angular-moment
On the moment-timezone home page, there are available three different distributions of moment-timezone.
moment-timezone.js is just the script. It contains no time zone data. If you use this distribution, it's expected that you will pull in your own time zone data, either by moment.tz.add, or moment.tz.load, as described in the docs.
moment-timezone-with-data.js includes all known time zone data from the tz database, for the version mentioned on the web site.
moment-timezone-with-data-2012-2022.js includes the same tz data, but is truncated to just years 2012 through 2022. This is a much smaller data file, and is sufficient for the majority of browser-side applications.
There are minified versions of each as well.
So if you are getting "Moment Timezone has no data for America/New_York", since America/New_York is a valid TZ database identifier, then you simply haven't loaded the data for it. You are probably using moment-timezone.js without adding data to it. Either include time zone data with moment.tz.add, or (more appropriately) switch to one of the files that already includes all time zone data.
However, do not do both. Time zone data should only be loaded once, and the moment-timezone scripts should only be loaded once. If you use either moment-timezone-with-data.js or moment-timezone-with-data-2012-2022.js, you should not be using moment-timezone.js, as that script is already included.
I'm using momentjs lib to updated text on some ajax action. What I need to do is to set a current date & time in london. I'm using moment.utc() function but because of the summer time I'm one hour out.
For example running this on 14:26
console.log( moment.utc().format('HH:mm:ss') );
I'm getting 13:26:53.
Any idea on how to fix this?
Can you use momentJS timezone?
moment().tz('Europe/London');
EDIT: In case you try to use this without seeing the link, it's a separate library you have to include.
If you want the local time instead of the UTC time, just use moment() instead of moment.utc(). You're specifically asking for UTC, so you shouldn't be surprised when you get UTC :)
From the documentation:
By default, moment parses and displays in local time.
If you want to parse or display a moment in UTC, you can use moment.utc() instead of moment().
This brings us to an interesting feature of Moment.js. UTC mode.
While in UTC mode, all display methods will display in UTC time instead of local time.
This is assuming you always want the user's local time. If you want a specific time zone (London) which may not be the user's time zone and isn't UTC, then you should use the library indicated by Takuya's answer. I would think carefully before doing so though - while it may be a sensible approach, you should at least validate that first. It's often reasonable to display a time for user U1 in the time zone of user U2 - but here you're using a fixed time zone. That's only appropriate if you know that U2 will always be in London. It would be really confusing if actually U2 is in some other zone - either the same as or different to U1.
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?
You can set the default timezone in Moment by using:
moment.tz.setDefault(String);
For example
moment.tz.setDefault("America/New_York");
npm install moment-timezone
var moment = require('moment-timezone'); and use this object
instead of usual moment.
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/
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.
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.
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);
From my understanding, Arshaw FullCalendar displays events according to the timezone of the local computer's operating system. I assume this is done by the javascript Date() object, which also is based on the local computer's operating system. I have an application that has group calendars and a group timezone, I want to be able to force every local Arshaw Calendar to display according to that group's time-zone, no matter what timezone that computer is. How do you do this?
Note: I've looked through the documentation fairly thoroughly, and found no such option. I'm hoping that javascript has something equivalent to php's date_default_timezone_set(), which seems to me the way this could be solved.
*Edit 1/31/2013 12:23pm CST:
I am sending everything to the calendar as unix timestamps, so I assume the option ignoreTimezone would not apply here, as described in this stackoverflow thread:
jQuery FullCalendar timezone synchronization
You should probably try to set the option "ignoreTimezone" to "false" and give to Arshaw FullCalendar date in ISO8601.
You can get more information about that here: http://arshaw.com/fullcalendar/docs/event_data/ignoreTimezone/
To convert unix timestamps to ISO8601, you can use this in javascript:
var d = new Date(1360412434000).toISOString();
This is ECMAScript 5.
See here for compatibility and fallback code: https://developer.mozilla.org/en-US/docs/JavaScript/Reference/Global_Objects/Date/toISOString