Difference of hours between two dates with JavaScript?

9

Hello.

I have these two dates:

   var dtPartida = "20170620 11:20";
   var dtChegada = "20170620 16:40";

You need to find out the difference in hours between these dates in the case and 5 hours and 20 minutes.

I need it to return like this:

  

5h 20m

and not like this:

  

05:20

    
asked by anonymous 16.06.2017 / 18:08

3 answers

7

Manually with javascript, you can create a date from the string, taking separately year, month, day, minute and second, and then calculate the difference between the dates (and minutes and seconds).

Example

var dtPartida = "20170620 11:20";
var dtChegada = "20170620 16:40";

var date1 = new Date(dtPartida.slice(0,4), dtPartida.slice(4,6),dtPartida.slice(6,8), dtPartida.slice(9,11), dtPartida.slice(12,14)),
    date2 = new Date(dtChegada.slice(0,4), dtChegada.slice(4,6),dtChegada.slice(6,8), dtChegada.slice(9,11), dtChegada.slice(12,14));

var diffMs = (date2 - date1);
var diffHrs = Math.floor((diffMs % 86400000) / 3600000);
var diffMins = Math.round(((diffMs % 86400000) % 3600000) / 60000);
var diff = diffHrs + 'h ' + diffMins + 'm';
console.log(diff);
    
16.06.2017 / 19:32
10

A powerful javascript library for handling dates Moment.js

  var dtChegada  = "20/06/2017 16:40:00";
  var dtPartida = "20/06/2017 11:20:00";

  var ms = moment(dtChegada,"DD/MM/YYYY HH:mm:ss").diff(moment(dtPartida,"DD/MM/YYYY HH:mm:ss"));
  var d = moment.duration(ms);
  var s = Math.floor(d.asHours()) + moment.utc(ms).format(":mm:ss");

console.log(s);
<script type="text/javascript" src="//cdnjs.cloudflare.com/ajax/libs/moment.js/2.4.0/moment.min.js"></script>
  

To return in the requested format in comment 5h 20m   just act on the variable s

     

var s = Math.floor(d.asHours()) + "h" + moment.utc(ms).format(" mm") +"m";

  var dtChegada  = "20/06/2017 16:40:00";
  var dtPartida = "20/06/2017 11:20:00";

  var ms = moment(dtChegada,"DD/MM/YYYY HH:mm:ss").diff(moment(dtPartida,"DD/MM/YYYY HH:mm:ss"));
  var d = moment.duration(ms);
  var s = Math.floor(d.asHours()) + "h" + moment.utc(ms).format(" mm") +"m";

console.log(s);
<script type="text/javascript" src="//cdnjs.cloudflare.com/ajax/libs/moment.js/2.4.0/moment.min.js"></script>

Assuming the times you mentioned before editing the question:

var dtChegada  = "16:40";
var dtPartida = "11:20";

var ms = moment(dtChegada,"HH:mm").diff(moment(dtPartida,"HH:mm"));
var d = moment.duration(ms);
var s = Math.floor(d.asHours()) + "h" + moment.utc(ms).format(" mm") +"m";
  
console.log(s);
<script type="text/javascript" src="//cdnjs.cloudflare.com/ajax/libs/moment.js/2.4.0/moment.min.js"></script>

In case the dates are in the format of Lucas Costa's response

  var dtChegada  = "20170620 16:40";
  var dtPartida  = "20170620 11:20";

  var ms = moment(dtChegada,"DD/MM/YYYY HH:mm:ss").diff(moment(dtPartida,"DD/MM/YYYY HH:mm:ss"));
  var d = moment.duration(ms);
  var s = Math.floor(d.asHours()) + "h" + moment.utc(ms).format(" mm") +"m";
  
  console.log(s);
<script type="text/javascript" src="//cdnjs.cloudflare.com/ajax/libs/moment.js/2.4.0/moment.min.js"></script>
    
16.06.2017 / 18:46
3

You can test for the minutes or seconds of the hours:

function verificarDiferencaHorario(inicialMin, finalMin) {
        var totalMin = Number(finalMin - inicialMin);
        console.log(Math.trunc(totalMin / 60).toString() + ":" + (totalMin % 60).toString());
    }

Assuming the times you mentioned:

function verificarHorario() {
        var inicial = "11:20", final = "16:40";
        var splInicial = inicial.split(":"), splFinal = final.split(":");

        var inicialMin = (Number(splInicial[0] * 60)) + Number(splInicial[1]);
        var finalMin = (Number(splFinal[0] * 60)) + Number(splFinal[1]);

        verificarDiferencaHorario(inicialMin, finalMin);
    }

The same goes for days: the last conversion in minutes and the checkDifferentialTime function returns the difference in hours and minutes.

    
16.06.2017 / 19:05