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

171
Views
Compare two times range JavaScript

I want to compare two times and show result as a boolean. I created a function for that. It is working. But when compare the "AM" and "PM" output is wrong.

let Time = "11:25 PM";
let currentTime = "10:00 AM";

console.log(isFutureTime(currentTime, Time));
//Output should be true or false

  function isFutureTime(currentTime, checkTime) {
    if (checkTime.split(" ")[1] == currentTime.split(" ")[1]) {
      if (parseInt(currentTime.split(":")[0]) < parseInt(checkTime.split(":")[0])) {
        return true;
      }
      else if (parseInt(currentTime.split(":")[0]) == parseInt(checkTime.split(":")[0])) {
        if (parseInt(currentTime.split(":")[1].substring(0, 2)) <= parseInt(checkTime.split(":")[1])) {
          return true;
        } else {
          return false;
        }
      } else {
        return false;
      }
    }
    else {
      return false;
    }
  }
about 4 years ago · Juan Pablo Isaza
2 answers
Answer question

0

You need to check whether currentTime is AM or PM, and return true if it's PM.

...
return currentTime.split(" ")[1] === 'PM';
...

Since you already checked both times are equal, You dont need to check checkTime.

You man declare a variable for currentTime.split(" ")[1] for efficiency.

let Time = "11:25 PM";
let currentTime = "10:00 AM";

console.log(isFutureTime(currentTime, Time));
//Output should be true or false

function isFutureTime(currentTime, checkTime) {
  if (checkTime.split(" ")[1] == currentTime.split(" ")[1]) {
    if (parseInt(currentTime.split(":")[0]) < parseInt(checkTime.split(":")[0])) {
      return true;
    } else if (parseInt(currentTime.split(":")[0]) == parseInt(checkTime.split(":")[0])) {
      if (parseInt(currentTime.split(":")[1].substring(0, 2)) <= parseInt(checkTime.split(":")[1])) {
        return true;
      } else {
        return false;
      }
    } else {
      return false;
    }
  } else {
    return currentTime.split(" ")[1] === 'PM';
  }
}

about 4 years ago · Juan Pablo Isaza Report

0

Alternatively, Javascript Date can natively parse time format you're using, though. So, I just need to concatenate a date string and construct a Date instance for both values. Then compare them.

function isFutureTime(currentTime, checkTime) {
  let date = new Date()
  date = `${date.getFullYear()}-${date.getMonth() + 1}-${date.getDate()}`

  return new Date(`${date} ${currentTime}`) > new Date(`${date} ${checkTime}`)
}

const currentTime = '10:00 AM';
const futureTime = '11:25 PM';
const pastTime = '03:15 AM';

console.log(futureTime, '=', isFutureTime(currentTime, futureTime))
console.log(pastTime, '=', isFutureTime(currentTime, pastTime))

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!