Time difference between two dates with Javascript?

Asked

Viewed 9,259 times

9

Hello.

I have these two dates:

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

And I need to figure out the difference in time between those dates that in the case and 5 hours and 20 minutes.

I need you to come back like this:

5h 20m

and not like this:

05:20

  • Add your code @Newtech!

  • Sorry! I updated the question to make it easier to understand.

  • var dtPartida = 20/06/2017 11:20 ??

  • @Leocaracciolo Sim

  • edited the answer, see there

  • @Leocaracciolo Stayed show. You know if you have how to return like this: 5h 20m?

  • see there in response

  • What is the format of the date to be processed? by the chosen answer I think it was doubtful if it is 20170620 11:20 or as you said in the comment "@Leocaracciolo Yes - Newtech 7 hours ago". Of qq way to return (5h 20m) it was only to remove the : (two points)there of the expression and to give a space var s = Math.floor(d.asHours()) + "h" + Moment.utc(ms). format(" mm") +"m"; re-edited the response

Show 3 more comments

3 answers

13

A powerful javascript library for date manipulation 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 the 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>

Supposing the times you mentioned before the question was edited:

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 reply

  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>

7


Manually with javascript, you can create a date from the string, picking separately year, month, day, minute and second, and then calculate the difference between 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);

4

You can test by minutes or seconds of hours:

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

Supposing 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 function checkDiferencaHorario returns in hours and minutes the difference.

Browser other questions tagged

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