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

124
Views
Validate string time range

I need to create a function to validate the following situation:

  1. Monitored interval inside opening hours: Valid

  2. Monitored interval outside opening hours: Invalid

There is another possibility that the allowed starting range is 23:00 to 04:00. In this case, ranges from 5:00 to 6:00 and 21:00 to 22:00 will not be allowed.

Currently my code validates the day normally, but the crossed day does not.

export default function isOutRangeStringTime({
  initialTime,
  finalTime,
  allowedInitialTime,
  allowedFinalTime,
}) {
  if (allowedInitialTime === allowedFinalTime
    || allowedInitialTime >= allowedFinalTime
    || initialTime >= finalTime) {
    return false;
  }
  return initialTime < allowedInitialTime || finalTime > allowedFinalTime;
}

Here's a picture of the situation that my code doesn't work.

  1. crossed day Valid: Crossday Valid

  2. crossed day invalid: enter image description here

I need validation to work for cross days too.

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

0

You can consider adding 24h to allowedFinalTime when allowedFinalTime < allowedInitialTime

Consider using Date Objects. For the sake of simplicity this example work with numbers:

function isOutRangeStringTime(
  initialTime,
  finalTime,
  allowedInitialTime,
  allowedFinalTime,
) {
  if (allowedInitialTime === allowedFinalTime
    || initialTime === finalTime) {
    return false;
  }
 if( allowedInitialTime > allowedFinalTime ) {
     allowedFinalTime += 24
     initialTime = (initialTime < allowedInitialTime) ? initialTime + 24 : initialTime;
     finalTime = (finalTime < allowedInitialTime) ? finalTime + 24 : finalTime;
 }

    return initialTime > allowedInitialTime && finalTime < allowedFinalTime;
}

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!