Get minutes between two times

Asked

Viewed 72 times

1

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(">"+d);
<script src="https://momentjs.com/downloads/moment.min.js"></script>

4 answers

0

The best way for you to solve it is to convert the times to seconds and subtract the difference. Ex: Take the 2 times, convert them into seconds, subtract the difference, take the difference result and convert it into minutes.

You see, this is a fairly well-known problem, which has been solved and documented and standardized as POSIX TIME

https://en.wikipedia.org/wiki/Unix_time

There are also libraries ready to do this job. One of my favorites is Momentjs

https://momentjs.com/

Good luck !

0

Why don’t you use asMinutes() directly on d? Just use this method and it returns the difference in minutes previously calculated between, in this case, dtChegada and dtPartida.

Take the example:

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

var ms = moment(dtChegada,"HH:mm").diff(moment(dtPartida,"HH:mm"));
var d = moment.duration(ms); // use neste "d" o asMinutes()
var s = Math.floor(d.asHours()) + "h" + moment.utc(ms).format(" mm") +"m";
                  
console.log(">"+s);
console.log("Em minutos: " + d.asMinutes())

var dtChegada2  = "19:27";
var dtPartida2 = "06:20";

var ms2 = moment(dtChegada2,"HH:mm").diff(moment(dtPartida2,"HH:mm"));
var d2 = moment.duration(ms2);
var s2 = Math.floor(d2.asHours()) + "h" + moment.utc(ms2).format(" mm") +"m";

console.log(">"+s2);
console.log("Em minutos: " + d2.asMinutes())
<script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.29.1/moment.min.js" integrity="sha512-qTXRIMyZIFb8iQcfjXWCO8+M5Tbc38Qi5WzdPOYZHIlZpzBHG3L3by84BBBOiRGiEb7KKtAOAs5qYdUiZiQNNQ==" crossorigin="anonymous"></script>

0

Function with Pure js (without Moment)

function diffMin(start, end) {
    var diff = 0;
    start = start.split(':');
    end = end.split(':');
    diff = (new Date(null, null, null, start[0], start[1]).getTime() - new Date(null, null, null, end[0], end[1]).getTime());
    diff = Math.abs((diff/1000)/60)
    return diff;
}
var min = diffMin("16:40", "11:20");
console.log(min);

0

Instead of using asHours and round with Math.floor, you can just use hours (as indicated in the documentation, the getters that begin with as return the full duration, while its versions without as return already rounded values).

Same goes for minutes: instead of using moment.utc, use minutes. The formatting would look like this:

var s = d.hours() + "h " + d.minutes().toString().padStart(2, '0') +"m";

And to get the total in minutes, you can use asMinutes:

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 = d.hours() + "h " + d.minutes().toString().padStart(2, '0') +"m";

console.log("Formatado: " + s);
console.log("Total em minutos: " + d.asMinutes());
    <script src="https://momentjs.com/downloads/moment.min.js"></script>

I just used padStart in the minutes to be shown with zero on the left if the value is less than 10 (you can do the same with the hours if you want).


Attention: do not use moment.utc in this case

Remember that when using moment.utc you are actually creating a date (and no longer a duration), which coincidentally has the value of minutes equal to the amount of minutes of the duration. But in general you should not mix things.

A date/time represents a point in the timeline (a date is a specific point in the calendar, a time is a specific time of the day). A duration is a quantity of time, without any relation to calendars ("it is two in the afternoon" is a time, a specific time of the day, already "the film has two hours of duration" is a duration: a quantity of time, does not say what hours start or end, only how long lasts).

That’s why there’s a moment.duration, since durations are different from a date, and you shouldn’t mix things up, even if "it works".


But of course you can also do without moment:

function totalMinutos(horario) {
    const [h, m] = horario.split(':').map(v => parseInt(v));
    return h * 60 + m;
}

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

// diferença dá o total em minutos
var diff = totalMinutos(dtChegada) - totalMinutos(dtPartida);
console.log(`Total em minutos: ${diff}`);
var h = Math.floor(diff / 60);
var m = diff % 60;
var formatado = `${h}h ${m.toString().padStart(2, "0")}m`;
console.log(`Formatado: ${formatado}`);

Browser other questions tagged

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