How do I get a date in js with the desired utc (+3)?
You need to return the time from + 3 hours to utc
1
Author: Эмиль Кулуев, 2020-03-27
1 answers
The bottom line is that js
we take time not from the server, but from the user's computer.
Accordingly, if the user changes the time on the computer / smartphone/tablet, or it is not set correctly, then you will receive incorrect data.
In your case, you can use getting the time from the server via php
, another YAP, or framework, and so on, or get the world time by API
Added an example implementation with comments for understanding how the script works:
let plus = 3; // Сколько времени прибавляем
var xhr = new XMLHttpRequest();
xhr.open('GET', 'http://worldtimeapi.org/api/timezone/Europe/London', false); // Делаем запрос по Лондону
xhr.send(); // отправляем
if (xhr.status != 200) {
console.log( xhr.status + ': ' + xhr.statusText ); // Если статус не равен 200, то выводим ошибку.
} else {
let time = xhr.responseText; // получаем текст ответа
let z = JSON.parse(time).utc_datetime; // Получаем время utc
let time1 = new Date(z).getTime(); // Переводим в timestamp
let timestampPlus = time1 + (plus * 60 * 60 * 1000); // Воемя +3 часа
let timePlus = new Date(timestampPlus); // Переводим во время (Тут надо понимать, что система сама переведёт его в текущую временную зону
let result = timePlus.toUTCString(); // Переводим в строку UTC;
console.log(result); // Выводим дату.
}
1
Author: Denis640Kb, 2020-03-27 18:01:29