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

150
Visualizações
Check if half of a period has passed

I have a system where the user can choose a period where after this period he earns for example + 2% of the sum he has chosen. If he tries to make an action with this sum before the half of the period it loses a percentage of its value. So what I want to know is how to calculate half of a date.

In my example:

I have for example in a mysql table the date when he put the sum and the date when the defined period will be finished. So if it is 24-2-2022 and he chooses in 6 months, there will be two pieces of information in the database 24-2-2022 and 24-8-2022. What I've done so far is split() the date to separate day, month and year. But here I am stuck because I do not know how to compare all this to see if the period is halfway or not.

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

0

You can write a function. But first you must convert your string to a valid date string. then you can use the Date Object.

d1 = "24-2-2022"
d2 = "24-8-2022"


function getDays(d1, d2) {
  _d1 = d1.split('-');
  _d2 = d2.split('-');


  date_1 = new Date(_d1[2] + '-' + _d1[1] + '-' + _d1[0])
  date_2 = new Date(_d2[2] + '-' + _d2[1] + '-' + _d2[0])

  return Math.floor((date_2 - date_1) / (1000*60*60*24));
}

console.log(getDays(d1, d2) + " days")  

about 4 years ago · Juan Pablo Isaza Relatório

0

You could use the day, month and year values to compare if the difference between the start day and today is bigger than half of the difference between the start and the end day :

let startDate = new Date(2022,1,24); //months are 0-indexed
let endDate = new Date(2022,7,24);
let halfDif = (endDate - startDate) / 2;
//new Date() => today
let isTodayAtLeastHalfway = new Date() - startDate >= halfDif;
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