Преобразование IST в GMT в JavaScript: методы и примеры

Чтобы преобразовать дату и время из IST (индийского стандартного времени) в GMT (среднее время по Гринвичу) в JavaScript, вы можете использовать различные методы. Вот несколько вариантов:

Метод 1: использование встроенного метода toLocaleString

const dateIST = new Date(); // Create a new Date object representing the current time in IST
const dateGMT = dateIST.toLocaleString("en-US", { timeZone: "GMT" }); // Convert the date to GMT
console.log(dateGMT);

Метод 2: использование метода toLocaleStringс параметром timeZone

const dateIST = new Date(); // Create a new Date object representing the current time in IST
const options = { timeZone: "GMT", timeZoneName: "short" };
const dateGMT = dateIST.toLocaleString("en-US", options); // Convert the date to GMT
console.log(dateGMT);

Метод 3. Использование метода toLocaleStringс параметрами timeZoneи hour12

const dateIST = new Date(); // Create a new Date object representing the current time in IST
const options = { timeZone: "GMT", hour12: false };
const dateGMT = dateIST.toLocaleString("en-US", options); // Convert the date to GMT
console.log(dateGMT);

Метод 4. Использование метода toLocaleStringс параметрами timeZoneи timeZoneName(для старых браузеров)

const dateIST = new Date(); // Create a new Date object representing the current time in IST
const options = { timeZone: "Etc/GMT" };
const dateGMT = dateIST.toLocaleString("en-US", options); // Convert the date to GMT
console.log(dateGMT);

Метод 5: использование метода getTimezoneOffset

const dateIST = new Date(); // Create a new Date object representing the current time in IST
const offsetIST = dateIST.getTimezoneOffset(); // Get the offset in minutes
const offsetGMT = -offsetIST / 60; // Convert the offset to hours and negate it
const dateGMT = new Date(dateIST.getTime() + offsetGMT * 60 * 60 * 1000); // Add the offset to the date
console.log(dateGMT);