Add minutes in Javascript

Asked

Viewed 4,280 times

2

I’m doing something for hours and minutes sum calculation, and I see no solution.

I need to get a time stamp formatted like this 09:00, but with random time on a input and by clicking Submit change html with input time with 70 minutes added. I couldn’t get past that part and I haven’t even tried to add up the minutes.

Someone could help me on this mission?

<html>
<head>
<title></title>
<span> hora inicio </span>
    <script type="text/javascript">
      function hora1(){
    var horas = getElementById("hrs")
    var hr1 = horas.split(":");    // a hora sera colada com ":" no input, entao preciso dar um split, separar minutos de hora e somar 70 minutos. 
    var hr2 = hr1[0]+":"+hr1[1]; // ainda preciso somar os minutos e mostrar na exibição final com os 70 minutos a mais dessa hora do input
 m.innerHTML = hr2;
} 
    </script>
</head>
<body>
<div id="m">
  09:50
</div>
<input type="text" id="hrs">
  <button onclick="hora1()">TESTAR</button>
</body>
</html>

I got results but without the possibility of typing the time, which is not what I needed

<!--          EM JAVASCRIPT EM UM ARQUIVO PHP COM RESULTADO OK MAS RECEBENDO A HORA DO SISTEMA -->
<html>
<body>
<h2>Somar 70 minutos</h2>
<script type="text/javascript"> 
var mc = 1; 
var mc5 = 20;
var d1 = new Date(); 
var d2 = new Date();
d1.setHours(+d2.getHours()+(mc) ); 
d1.setMinutes(new Date().getMinutes()+(mc5) ); 
document.write("#resultado: " + " " + d1.getHours()+":"+d1.getMinutes());
</script>
</body>
</div>
</html> 
  • What would be random horá ??

  • EXAMPLES: 07:53 or 20:35

3 answers

5

You can use the Javascript Date object. First you must convert the received time to a timestamp. This can be done as follows:

function toTimestamp(horario){
  var aux = horario.split(':'), dt = new Date();
  dt.setHours(aux[0]);
  dt.setMinutes(aux[1]);
  dt.setSeconds(0);
  return dt.getTime();
}

The above function, takes a time and returns a timestamp. A timestamp is the date/time converted into milliseconds (this in Javascript). So we can set 70 minutes with a timestamp too, just convert to milliseconds.

var minutosAdd = 70*60*1000; // 70 minutos em milissegundos

Now that you have the timestamp of the past hour and the timestamp of 70 minutes, just add them up. We can have the function 70 minutes more() that takes a string with the time and returns another string with the time passed summed with the 70 minutes.

function mais70minutos(hora){
  var timeHoraFinal = toTimestamp(hora) + minutosAdd;
  var dt = new Date(timeHoraFinal);
  var horaRetorno = (dt.getHours() < 10) ? '0'+dt.getHours() : dt.getHours();
  horaRetorno += (dt.getMinutes() < 10) ? '0'+dt.getMinutes(): dt.getMinutes();
  return horaRetorno;
 }

Using these functions the way you think best it is possible that your problem will be reversed. I hope I helped. Check on https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date

2

Use the Moment js. to make your job easier.

Take an example:

 var hora = documento.getElementById('hrs');

 var resultado = moment(hora, 'hh:mm').add(70, 'minutes').format('hh:mm')

If the hours of input were 11:30, the result will be 12:40.

Explaining the code

The second function parameter moment receives the date input format. The function add adds an amount of time, and the second parameter informs the type (in case it could be hours, month, years and the like), but we need to use minutes. And it’s over, the format allows you to put the output in the required format.

Documentation of the Momentjs

0

where did I go wrong? failed, returns invalid date:

<html>
<head>
    <script src="moment.js"></script>
</head>
<body>
<h2>Somar 70 minutos</h2>
<script type="text/javascript">
function fff(){
 var hora = document.getElementById('hrs');
 var resultado = moment(hora, 'hh:mm').add(70, 'minutes').format('hh:mm');
    document.getElementById("hrs2").innerHTML = resultado;
}
</script>
<p id="hrs2"></p>
<input type="text" id="hrs">
  <button onclick="fff()">TESTAR</button>
</body>
</html> 
  • var hora = Document.getElementById('hrs'). value;

  • var hora = Document.getElementById('hrs'). value;

Browser other questions tagged

You are not signed in. Login or sign up in order to post.