#
#
# artur #2016-02-03 03:48:13
JavaScript
# artur #, 2016-02-03 03:48:13

How to respect time zone when working with Moment.js?

Hello!
There was a problem with temporary zones of users. I spent the whole day to understand how Moment.js works and why it is so praised.
The problem is in the current date, for some reason it shows 1 hour less than it should be.
I set the global locale and time zone:

moment.locale('ru');
moment.tz.setDefault("Europe/Moscow");

I'm trying to display the current time, taking into account the time zone:
var now = moment().format('MMMM Do YYYY, HH:mm:ss');
console.log(now) // получаю Февраль 3-го 2016, 02:36:51

Although it should show February 3rd 2016, 03:36:51 - this is the time on the PC and this is the exact Moscow time at the moment.
When changing the time on the PC upwards (04:36:51), it shows correctly 03:36:51 ... and so on with each time zone.
I'm probably missing something, can anyone help clarify?
I roughly figured out the dates from the past, it’s easier there. I store the date in the database in the UTC time zone and Moment.js simply adds or subtracts the required number of hours depending on the set time zone:
var date = moment.utc('2016-01-05 12:00:00').format(); // Как-бы дата из прошлого
var converted = moment(date).format('MMMM Do YYYY, HH:mm:ss'); // Форматируем с учетом установленной глобальной временной зоны
console.log('было - ' + date + ', стало - ' + converted) // было - 2016-01-05 12:00:00, стало - 2016-01-05 15:00:00

- Am I doing everything right or have I gone in the wrong direction? In the last example, the first line of .utc confuses me , if you do not specify this parameter, then it adds +3 hours (Europe/Moscow) to the date from the past, of course, this is due to the set global time zone, but I can’t do without it because. to. there are a lot of dates and you need to adjust them, you won’t constantly register, you want to indicate in one place.
I was also confused by the function for determining the client time zone:
moment.tz.guess();
, which gives an error:
moment.js:209 Uncaught TypeError: Cannot read property 'join' of null
on this line, the following code:
abbr = abbr[0].match(/[A-Z]/g).join('');
- I tried to remove the quotes inside the Join, but it didn't help, it's likely that the array is empty and there is nothing to split, it turns out that this function could not determine my time zone... I'm using Google Chrome Version 48.0.2564.97 m
Please help me figure it out, or could you tell me an alternative version of the plugin for working with the past, future and present, taking into account time zones?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
B
bromzh, 2016-02-03
@passshift

The moment takes information about time and time zone from the OS. If it shows something wrong, then you have the time zone configured incorrectly in the OS.
Example: I changed tz to Paris (+01:00), but set the time to Moscow. If you request a moment of time, then it will give out what is installed in the system: 2016-02-03T04:08:24+01:00. The time coincides with Moscow, but the wrong tz. If you forcibly set the time zone at the very moment, then it will take the system time and tz and try to translate it into local time for the specified tz. Thus, if you set the Moscow tz and request the current time again, the moment will return 2016-02-03T06:13:17+03:00. Time has shifted, but tz is correct.
So you most likely have tz +04:00 in your system, so the hour is lost.
Well, yes, the time must be stored in UTC, and displayed on the page in accordance with the user's time zone. The time zone can be checked with the client himself.
You can find out the offset in minutes relative to UTC like this: moment().utcOffset()

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question