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

256
Views
How to check if a timeslot lies between two other timeslots?

I have 3 time stamps

StartTime: 22:30:00

EndTime: 05:00:00

CurrentTime: 03:00:00

Now if the current time lies between StartTime and EndTime, then I want to print the "CurrentTime". So in the above example since CurrentTime is between 22:30:00 and 05:00:00, hence I wish to print it.

So I used the following code

const StartTime = new Date("2019-02-22T23:00");
const EndTime = new Date("2019-02-23T05:00");
const CurrentTime = new Date("2022-02-23T03:00");

const StartTimeHours = StartTime.toLocaleTimeString('en-US', { hour12: false });
const EndTimeHours = EndTime.toLocaleTimeString('en-US', { hour12: false });
const CurrentHours = CurrentTime.toLocaleTimeString('en-US', { hour12: false});

if (StartTimeHours < CurrentHours && CurrentHours < EndTimeHours)
{
    console.log("Current Time",CurrentHours);
}

However, since 05:00:00 is less than 22:30:00, hence it is not printing the output. So how do I make it check that the EndTime is between the two time slots

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

0

There were some minor issues in your code,
but you can try checking on the full date-time values, like so:

const StartTime = new Date("2022-02-22T23:00");
const EndTime = new Date("2022-02-23T05:00");
const CurrentTime = new Date("2022-02-23T03:00");

const StartTimeHours = StartTime.toLocaleTimeString('en-US', { hour12: false });
const EndTimeHours = EndTime.toLocaleTimeString('en-US', { hour12: false });
const CurrentHours = CurrentTime.toLocaleTimeString('en-US', { hour12: false});

if (StartTime < CurrentTime && CurrentTime < EndTime)
{
    console.log("Current Time",CurrentHours);
}

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!