Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

JavaScript - Get minutes between two dates

If I have two dates, how can I use JavaScript to get the difference between the two dates in minutes?

like image 784
John Avatar asked Oct 16 '22 10:10

John


People also ask

How do I calculate minutes between two dates?

To calculate the minutes between two times, multiply the time difference by 1440, which is the number of minutes in one day (24 hours * 60 minutes = 1440).

How can I get minutes between two dates in SQL?

SQL Server DATEDIFF() Function The DATEDIFF() function returns the difference between two dates.


1 Answers

You may checkout this code:

var today = new Date();
var Christmas = new Date(today.getFullYear() + "-12-25");
var diffMs = (Christmas - today); // milliseconds between now & Christmas
var diffDays = Math.floor(diffMs / 86400000); // days
var diffHrs = Math.floor((diffMs % 86400000) / 3600000); // hours
var diffMins = Math.round(((diffMs % 86400000) % 3600000) / 60000); // minutes
console.log(diffDays + " days, " + diffHrs + " hours, " + diffMins + " minutes until Christmas =)");

or var diffMins = Math.floor((... to discard seconds if you don't want to round minutes.

like image 289
Darin Dimitrov Avatar answered Oct 19 '22 00:10

Darin Dimitrov