Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

174
Visualizações
How can I set equality condition for Date now and a pre-defined date?

I am trying to set the condition below to run Condition1 if new Date(visitationDate) is equal to Date.now().

In the frontend the visitationDate come in as Thu Nov 11 2021 13:52:33 GMT+0100 (West Africa Standard Time) and I convert it with a function to the format 2021-11-11T13:00:00.000Z before sending it to the backend. I am using the date condition in my question in the backend. I can revert the conversion in the frontend if it will resolve my issue.

This is my current code used in the backend

if (new Date(visitationDate) == Date.now()) {
   Condition1
} else {
   Condition2
}

My question is how can I run Condition2 if the visitationDate is less than Date.now() or Date.now() is greater than visitationDate. I want to run Condition1 only if visitationDate is the current date or equal to Date.now() in a 24 hour window.

about 4 years ago · Juan Pablo Isaza
2 Respostas
Responde à pergunta

0

Based on a previously submitted response that was deleted, I am using the getHours() method to normalize the date and checking if the difference is equal to zero. I tested with the current date and it returns true. When I tested with previous date and future date it returns false. Please note that users of my code have the same local time.

let d = new Date(visitationDate);
d.setHours(15, 0, 0, 0)
let now = new Date(); 
now.setHours(15, 0, 0, 0) 

if (now-d == 0) {
   Condition1
} else {
   Condition2
}
about 4 years ago · Juan Pablo Isaza Relatório

0

It seems that you want to compare dates without the time part, so convert the input timestamp to just a date and compare it to the current local date, e.g.

// Convert standard toString format to YYYY-MM-DD without changing the date
function reformatDate(s) {
  // Parse string as UTC+0  
  let d = new Date(s.replace(/GMT\+.*/,'GMT+0000'));
  // Return date only in ISO 8601 format
  return d.toISOString().substring(0,10);
}

// Return date as local YYYY-MM-DD
function formatDate(d) {
  return d.toLocaleDateString('en-CA');
}

let ts = 'Thu Nov 11 2021 13:52:33 GMT+0100 (West Africa Standard Time)';
// Date with some random time
let d = new Date(2021,10,11,23,59,59);
console.log(formatDate(d) + ' : ' +  (reformatDate(ts) == formatDate(d)));

// Compare to today
d = new Date();
console.log(formatDate(d) + ' : ' +  (reformatDate(ts) == formatDate(d)));

about 4 years ago · Juan Pablo Isaza Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda