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

289
Visualizações
How to make countdown where it looks to the future, so when my date in the year is passed it should look to the next year

I'm making a countdown but right now it only gives a response if the date hasn't passed yet. But I want that if the date is passed already that it goes to the next year. How could I do that? I guess I need to use an "if" at my const with the date but I have no idea how to do that.

  const difference = +new Date(`01/01/${year}`) - +new Date();
about 4 years ago · Santiago Gelvez
1 Respostas
Responde à pergunta

0

Hello Simon, first of all, I want to tell you (as a tip) that you don't need to convert dates to calculate the difference between them. That being said, I recommend you use date-fns for date calculations, with this library you will be able to use methods like endOfDay(date). If you still don't want to use any external library, you can use setHours method:

const now = new Date()
const endOfDay = new Date(new Date(now.toString()).setHours(23, 59, 59, 999))
const isBeforeEndOfDay = now <= endOfDay

console.log(isBeforeEndOfDay)

And to get the difference between the two dates you don't need to calculate if its the end of the day:

// To set two dates to two variables
const date1 = new Date("06/30/2019");
const date2 = new Date("07/30/2019");
  
// To calculate the time difference of two dates
const Difference_In_Time = date2.getTime() - date1.getTime();
  
// To calculate the no. of minutes between two dates
const Difference_In_Days = Difference_In_Time / (1000 * 3600 * 24);

const Difference_In_Hours = Difference_In_Days * 24

console.log(Difference_In_Days, Difference_In_Hours)

// [...]

about 4 years ago · Santiago Gelvez 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