How to get moment-timezone current configured timezone? - javascript

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.

Related

How to get device's timezone with expo-localization?

expo-localization's documentations says this about the Localization.timezone:
Deprecated. Use Localization.getLocales() instead.
But the Localization.getLocales() returns a Locale object, which has the following attributes, none of which is timezone or anything that could help find it:
So how would I get a user's timezone with this package?
You may try this one
Intl.DateTimeFormat().resolvedOptions().timeZone
It is alternatively return the same thing as you are expected

moment locale not set correctly

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());

using moment js to pass in time zone and get hours offset?

I want to pass a three-digit time zone string like "GMT" or "EST" and get the offset like "-05:00" as a string from it.
Moment seems to handle it but I couldn't find a way...
This capability you are asking about has been deprecated. Tim Wood in this thread says:
The problem is that Date.prototype.toString returns such different results. That is the only place to get the timezone name (PST, CST, EST, etc). If this method is not returning any timezone information (as is the case with FF10 and IE9, there is no way to get it.
In that same thread there are mentions by some people about how they are able to get the abbreviated time zone and consequently pass it to moment but only in certain browsers.

Set global time zone

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);

Change timezone in Arshaw FullCalendar

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

Categories

Resources