
The little task is to print the date
I want to tell you about a simple task - the date of the conclusion on the main page with Mail.Ru . Small task, non-trivial solution.

The solution to the forehead:
Recently, another block appeared on the page. The world hockey championship has begun, and it is necessary to deduce the start of the match. But the time for each user must be synchronized with his time zone.

Also not too difficult - from the server we get the timestamp of the start of the match. On the client, we call the function we have already made.
However, it is not always possible to correctly determine the user's region automatically. For example, my home provider in our area began to issue IP addresses from a range that he did not have before. All portals offered me different places on our planet, but they all agreed on one thing: I am definitely located far from Moscow. It is for such situations that we suggest the user to choose a city on their own.
Naturally, all dates must be synchronized with the selected city, which means that the user's current offset relative to Greenwich may be incorrect.
We have a timestamp for the start of the match and the time zone of the selected city. On the client, we make a date and shift it by the difference between the current user offset and the offset in the selected city.
Why the plus between town_offset and date.getTimezoneOffset () can be found here: developer.mozilla.org/en/JavaScript/Reference/Global_Objects/Date/getTimezoneOffset#Description
Now, if the user in Vladivostok, but chose Moscow, we will adjust the Date object to the difference between these two cities.
We figured out the time of the start of the match, but there is also the current date. And not everything is so smooth with her. When displaying the match date, we use timestamp from the database, and when displaying the current date, we use new Date () on the client.
And this means that we use the timestamp of the user computer. The user can manually translate his watch, and then we get an incorrect current timestamp. Therefore, we also have to transfer the current timestamp from the server.
But this is not the end - the clock on the main page should be “ticking”, even though we cannot trust the user timestamp, but you can rely on the timestamp offset relative to the page loading. The fact that the user will knock down a local clock precisely in the middle of viewing the news on the main one will not be laid down.
Andrey Sumin
Head of Mail.Ru Client Development, Mail.Ru Group Company
Egor Dydykin
Timlid Mail.Ru Portal Home Development Team Mail.Ru Group

The solution to the forehead:
function print_date(date){
return date.getDate() + ' ' + getMonth(date.getMonth()) …
}
print_date( new Date() );
Recently, another block appeared on the page. The world hockey championship has begun, and it is necessary to deduce the start of the match. But the time for each user must be synchronized with his time zone.

Also not too difficult - from the server we get the timestamp of the start of the match. On the client, we call the function we have already made.
print_date( new Date(timestamp) );
However, it is not always possible to correctly determine the user's region automatically. For example, my home provider in our area began to issue IP addresses from a range that he did not have before. All portals offered me different places on our planet, but they all agreed on one thing: I am definitely located far from Moscow. It is for such situations that we suggest the user to choose a city on their own.
Naturally, all dates must be synchronized with the selected city, which means that the user's current offset relative to Greenwich may be incorrect.
We have a timestamp for the start of the match and the time zone of the selected city. On the client, we make a date and shift it by the difference between the current user offset and the offset in the selected city.
var date = new Date(timestamp);
date.setMinutes(town_offset + date.getTimezoneOffset())
print_date(date)
Why the plus between town_offset and date.getTimezoneOffset () can be found here: developer.mozilla.org/en/JavaScript/Reference/Global_Objects/Date/getTimezoneOffset#Description
Now, if the user in Vladivostok, but chose Moscow, we will adjust the Date object to the difference between these two cities.
We figured out the time of the start of the match, but there is also the current date. And not everything is so smooth with her. When displaying the match date, we use timestamp from the database, and when displaying the current date, we use new Date () on the client.
var date = new Date();
date.setMinutes(town_offset + date.getTimezoneOffset())
print_date(date)
And this means that we use the timestamp of the user computer. The user can manually translate his watch, and then we get an incorrect current timestamp. Therefore, we also have to transfer the current timestamp from the server.
var date = new Date(server_timestamp);
date.setMinutes(town_offset + date.getTimezoneOffset())
print_date(date)
But this is not the end - the clock on the main page should be “ticking”, even though we cannot trust the user timestamp, but you can rely on the timestamp offset relative to the page loading. The fact that the user will knock down a local clock precisely in the middle of viewing the news on the main one will not be laid down.
var begin = new Date().getTime();
function get_delta(){
return new Date().getTime() - begin;
}
function update_time(){
var date = new Date(server_timestamp + get_delta());
date.setMinutes(town_offset + date.getTimezoneOffset())
print_date(date)
}
Andrey Sumin
Head of Mail.Ru Client Development, Mail.Ru Group Company
Egor Dydykin
Timlid Mail.Ru Portal Home Development Team Mail.Ru Group