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

197
Views
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 answers
Answer question

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 Report

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 Report

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 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!