Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

172
Vistas
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 Respuestas
Responde la pregunta

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 Denunciar

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 Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda