Skip to content
Advertisement

How to compare a ZonedDateTime’s time with another ZonedDateTime but with only time and not date?

I have two dates in my Local Time time.

"2021-09-01 07:00:00 AM" and "2021-09-01 08:00:00 AM" (PST)

I’m trying to check if these two dates are between two times, 8 am and 10 pm of another time zone. (EST)

JavaScript

I’m stuck on how I can compare the starting time zones, startEst and endEst with the two times 8 am and 10 pm without the use of a date and only using the time.

I’ve added two more ZonedDateTimes to compare

JavaScript
JavaScript

Advertisement

Answer

There are many problems with your code.

  1. You should parse the strings to LocalDateTime as they do not have a timezone.
  2. Convert them to ZonedDateTime, which you have not done correctly. You should use LocalDateTime#atZone to do so.

Finally, convert the obtained ZonedDateTimes to the ZonedDateTimes in the EST timezone and then do the comparison. For getting the buisnessStartHoursInEst8am and buisnessEndHoursInEst10pm, use ZonedDateTime#withHours.

Do it as follows:

JavaScript

Learn more about the modern Date-Time API* from Trail: Date Time.

Advertisement