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

192
Visualizações
How to add minutes and hours to a time string using jquery

I want to add 30 minutes and then one hour to my variable which i already have my own date

var initialDate = '10:00';

So

if (some condition){
    // i add 30 minutes ->10:30
}elseif(another condition){
    // i add 1hour ->11:00
}

I tried this but doesn't work

var initialDate = '10:00';
var theAdd = new Date(initialDate);
var finalDate = theAdd.setMinutes(theAdd.getMinutes() + 30);
about 4 years ago · Santiago Trujillo
3 Respostas
Responde à pergunta

0

If I understand you correctly, the following will help you.

You need to add momentjs dependency via script tag and you can Parse, validate, manipulate, and display dates in JavaScript.

You can find more documentation regarding this in momentjs website

console.log(moment.utc('10:00','hh:mm').add(1,'hour').format('hh:mm'));

console.log(moment.utc('10:00','hh:mm').add(30,'minutes').format('hh:mm'));
<script src="https://momentjs.com/downloads/moment-with-locales.js"></script>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>

about 4 years ago · Santiago Trujillo Relatório

0

var theAdd = new Date();

// Set Hours, minutes, secons and miliseconds
theAdd.setHours(10, 00, 00, 000);

if (some condition) {
   // add 30 minutes --> 10:30
   theAdd.setMinutes(theAdd.getMinutes() + 30);
}
elseif (some condition) {
   // add 1 hour --> 11:00
   theAdd.setHours(theAdd.getHours() + 1);
}

Then you print the var theAdd to obtain the date and time.

To obtain just the time:

theAdd.getHours() + ":" + theAdd.getMinutes();
about 4 years ago · Santiago Trujillo Relatório

0

This should do the job. Dates need a year and month in their constructor, and you have to specify larger units of time if you specify and smaller ones, so it needs a day as well. Also, you have to pass in the hours and minutes separately. See https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date.

var initialDate = '10:00';
var theAdd = new Date(1900,0,1,initialDate.split(":")[0],initialDate.split(":")[1]);
if(30 min condition){
theAdd.setMinutes(theAdd.getMinutes() + 30);
} else if (1 hour condition){
theAdd.setHours(theAdd.getHours() + 1);
}
console.log(theAdd.getHours()+":"+theAdd.getMinutes());

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