Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

200
Vistas
Comparing dates with getTime() in EST timezone

I am looking if there is a better way (other than below solution) of comparing dates for all users (in different countries) in EST timezone. Need your inputs/help.

Basically I have to display three UI's based on three different dates. Till June, 1st UI has to render, June to July second UI and from July 3rd UI has to display. These dates while comparing should be in EST timezone.

  const JulyDate = new Date("2022-07-01T00:00:00.000-05:00");
  const JuneDate = new Date("2022-06-01T00:00:00.000-05:00");
  if(new Date().getTime() < JuneDate.getTime()){
    renderUIPriorJune= true;
    renderUIJune= false;
    renderUIJuly= false;
  }
  else if(new Date().getTime() >= JuneDate.getTime() && new Date().getTime() < JulyDate.getTime()){
    renderUIPriorJune= false;
    renderUIJune= true;
    renderUIJuly= false;

  }
  else if(new Date().getTime() >= JulyDate.getTime()){
    renderUIPriorJune= false;
    renderUIJune= false;
    renderUIJuly= true;

  }

I have defined June and July dates in EST timezone i.e. -5 hours as offset and then current date is compared with those dates.

Could you please let me know if this is the correct way of comparing or Is there a better way of doing. I am rather new to Java script.

Thank you in advance.

about 4 years ago · Juan Pablo Isaza
3 Respuestas
Responde la pregunta

0

The getTime() method always returns the number of milliseconds in UTC time zone - and this is exactly what you need in order to compare 2 timestamp values because UTC timezone does not have daylight saving periods.

about 4 years ago · Juan Pablo Isaza Denunciar

0

Your logic is fine, but a simpler method is to just get the current month in the required timezone. By "EST" I guess you mean US Eastern Standard Time, which corresponds to the IANA timezone "US/Eastern".

So all you need to do is find out which month the date falls in, which can be done using toLocaleString with suitable options, e.g.

function getEasternMonth(date = new Date()) {
  return date.toLocaleString('en',{month: 'numeric', timeZone:'US/Eastern'});
}

console.log('Current month in US EST: ' + getEasternMonth());

let s = '2022-06-01T00:00:00Z';
console.log(s + ' is EST month ' + getEasternMonth(new Date(s)));

Now your logic is hugely simplified to:

let month = getEasternMonth();
if (month < 6) {
  // do stuff for pre–June
} else if (month == 6) {
  // do stuff for June
} else {
  // do stuff for post June
}

Note that all the above is using the calendar month number (i.e. 6 = June).

about 4 years ago · Juan Pablo Isaza Denunciar

0

For any date manipulation or comparison, you should be using a library. A good one is dayjs.

To find if a date is before june 1st, you don't have to care about the time, just compare the dates. Dayjs has a method isBefore which takes granularity as one of the arguments. If you set the granularity to day, it will compare year, month and day, but not hour, minute, etc.

const now = dayjs();
const isBeforeJune = now.isBefore('2022-06-01', 'month')
about 4 years ago · Juan Pablo Isaza Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda