Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

387
Views
¿Cómo puedo calcular las horas de diferencia entre dos zonas horarias en nodeJS?

¿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! :(

about 4 years ago · Juan Pablo Isaza
2 answers
Answer question

0

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)}`));

about 4 years ago · Juan Pablo Isaza Report

0

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.

about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!