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

142
Vistas
If/Else condition using specific time ranges

I'm trying to formulate an if/else condition with the following criteria using javaScript:

"Good Morning" if the time is between 5:45 am and 11:59:59 am "Good Afternoon" if the time is between 12:00 pm and 05:59:59 pm "Good Evening" if the time is between 6:00 pm and 5:44:59 am

about 4 years ago · Juan Pablo Isaza
2 Respuestas
Responde la pregunta

0

You can use Date and get current hour with getHours function.

const d = new Date(); // get current date
const hour = d.getHours(); // get current hour

if (hour < 12)
  console.log("good morning");
else if (hour < 16)
  console.log("good afternoon");
else if (hour < 24)
  console.log("good evening");
  
 

If you want to work with minutes too. you can use getMinutes function.

const d = new Date(); // get current date
const hour = d.getHours(); // get current hour
const minute = d.getMinutes(); // get current minute

  if (hour < 12 && minute < 59)
    console.log("good morning and time is hour:" + hour + " minute:" + minute);
  else if (hour < 16)
    console.log("good afternoon and time is hour:" + hour + " minute:" + minute);
  else if (hour < 24)
    console.log("good evening and time is hour:" + hour + " minute:" + minute);

about 4 years ago · Juan Pablo Isaza Denunciar

0

If you convert the time to date format you can use getHours:

var d = new Date();
var n = d.getHours();

See how to work with date and time:

https://www.w3schools.com/jsref/jsref_gethours.asp

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