¿Cómo puedo calcular la diferencia entre UTC (que genero con new Date()) y la hora estándar europea?
Algo como
const amsterdam = new Date('Europe/Amsterdam') amsterdam.getTimezoneOffset() // returns the minutes of offset in this case 60¡No puedo simplemente usar 1 hora ya que el tiempo cambia en invierno y verano! :(
Puede obtener el desplazamiento para una ubicación en particular para cualquier fecha usando Intl.DateTimeFormat con opciones adecuadas, por ejemplo
/* @param {string} loc - IANA representative location * @param {Date} date - default to current date * @returns {string} offset as ±H[mm] */ function getOffsetForLoc(loc, date = new Date()) { // Use Intl.DateTimeFormat to get offset let opts = {hour: 'numeric', timeZone: loc, timeZoneName:'short'}; let getOffset = lang => new Intl.DateTimeFormat(lang, opts) .formatToParts(date) .reduce((acc, part) => { acc[part.type] = part.value; return acc; }, {}).timeZoneName; let offset = getOffset('en'); // If offset is an abbreviation, change language if (!/^UCT|GMT/.test(offset)) { offset = getOffset('fr'); } // Remove GMT/UTC return offset.substring(3); } // Get current offsets for following locations ['Europe/Amsterdam', 'America/New_York', 'Asia/Kolkata'] .forEach(loc => console.log(`${loc} : ${getOffsetForLoc(loc)}`)); // Get offsets in Amsterdam [new Date(2021,0), // 1 Jan 2021 new Date(2021,5) // 1 Jun 2021 ].forEach(d => console.log(`Offset for Amsterdam on ${d.toLocaleDateString()} ${getOffsetForLoc('Europe/Amsterdam', d)}`));Lo que finalmente terminé haciendo y funcionó perfectamente para mí fue lo siguiente
const timezoneOffsetInHours = moment().tz('Europe/Amsterdam').hour() - new Date().getHours()Esto devuelve las horas que Amsterdam está por delante de UTC.