JavaScript - Get Minutes Between Two Dates

JavaScript - Get minutes between two dates

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 =)");

Javascript return number of days,hours,minutes,seconds between two dates

Just figure out the difference in seconds (don't forget JS timestamps are actually measured in milliseconds) and decompose that value:

// get total seconds between the times
var delta = Math.abs(date_future - date_now) / 1000;

// calculate (and subtract) whole days
var days = Math.floor(delta / 86400);
delta -= days * 86400;

// calculate (and subtract) whole hours
var hours = Math.floor(delta / 3600) % 24;
delta -= hours * 3600;

// calculate (and subtract) whole minutes
var minutes = Math.floor(delta / 60) % 60;
delta -= minutes * 60;

// what's left is seconds
var seconds = delta % 60; // in theory the modulus is not required

EDIT code adjusted because I just realised that the original code returned the total number of hours, etc, not the number of hours left after counting whole days.

Calculate minutes between two dates

First convert date to milliseconds and then subtract and convert back to minutes.

for eg,


long startDateMillis = startDate.getTime();
long endDateMillis = endDate.getTime();

long diffMillis = startDateMillis - endDateMillis;

long minutes = (diffMillis/1000)/60;

How can I correctly calculate the number of minutes between these two dates in Javascript?

Try this,

NaN stands for "Not a Number", if you try to perform mathematical operations on the data type other than number, you generally see this error.

text file refers to the year 2001, see the output below

var date1 = new Date('Tue 29 Jun, 12:57 PM');
var date2 = new Date(Date.now());
diffInMinutes = Math.abs(Number(date1.getTime()) - Number(date2.getTime()))/60000;

//output
10559347.2098
date1
Fri Jun 29 2001 12:57:00 GMT+0530 (India Standard Time)
date2
Tue Jul 27 2021 10:04:12 GMT+0530 (India Standard Time)

// If you are referring the date to 2021 from the text file, try this

var textFileDate = 'Tue 29 Jun, 12:57 PM';
var appendYear = textFileDate.replace(',', ' ' + new Date().getFullYear()+',');
var textFileDate = new Date(appendYear);
var sysDate = new Date(Date.now());
diffInMinutes = Math.abs(Number(textFileDate.getTime()) - Number(sysDate.getTime()))/60000;

//output

40164.49135
textFileDate
Tue Jun 29 2021 12:57:00 GMT+0530 (India Standard Time)
sysDate
Tue Jul 27 2021 10:21:29 GMT+0530 (India Standard Time)

Difference between two dates in minute, hours javascript

Try:

var diffHrs = Math.floor((hourDiff % 86400000) / 3600000);

Math.round rounded the 0.5 hour difference up to 1. You only want to get the "full" hours in your hours variable, do you remove all the minutes from the variable with the Math.floor()

Finding the difference between 2 times in minutes

More simple with getTime() that represents the epoch time in milliseconds. So in that context, the minutes will be:

1 minute = 60 seconds = 60 0000 milliseconds

your code can be:

function diffMinutes(date1, date2) {
const d1 = new Date(date1).getTime();
const d2 = new Date(date2).getTime();
return Math.round((d2 - d1) / 60000); // Can use Math.floor or Math.ceil depends up to you
}

The code handles negative values if the second time is lower than the first, so if you want always positive values you can do:

    return Math.abs(Math.round((d2 - d1) / 60000));

Examples:

diffMinutes("01-01-2012 11:11:11", "01-01-2012 11:15:11") // 4
diffMinutes(new Date("01-01-2012 11:11:11"), "01-01-2012 11:00:11") // -11
diffMinutes(12323, 123213) // 2

If you know always you call "format_date" the params will be both Date object you can reduce your function to:

function diffMinutes(date1, date2) {
return Math.round((date2.getTime() - date1.getTime()) / 60000); // Can use Math.floor or Math.ceil depends up to you
}

Get hours difference between two dates in Moment Js

You were close. You just need to use the duration.asHours() method (see the docs).

var duration = moment.duration(end.diff(startTime));
var hours = duration.asHours();

Getting the difference between 2 dates in Javascript in hours, minutes, seconds with UTC

In your code you have:

var date1 = new Date().getTime();

There's no need to use getTime in this case. But it is helpful to use meaningful variable names:

var now = new Date();

Then there's:

var date2 = new Date("05/29/2017").getTime();

Don't use the Date constructor (or Date.parse) to parse strings as it's mostly implementation dependent and inconsistent. If you have a specific date, pass values directly to the constructor.

Regarding "use UTC", that's a bit confusing as ECMAScript Date objects are UTC. They use the host timezone offset to calculate an internal UTC time value, and also to display "local" date and time values. The only way I can interpret "use UTC" is to use Date.UTC to create a date instance, e.g.:

var endDate = new Date(Date.UTC(2017,4,29)); // 2017-05-29T00:00:00Z

Now you can get the difference between then an now using:

var diff = endDate - now; 

When trying to get the hours, minutes and seconds you have:

var seconds = diff / 1000;
var minutes = (diff / 1000) / 60;
var hours = minutes / 60;

That converts the entire difference to each of hours, minutes and seconds so the total time is about 3 times what it should be. What you need is just the components, so:

var hours = Math.floor(diff / 3.6e5);
var minutes = Math.floor(diff % 3.6e5) / 6e4);
var seconds = Math.floor(diff % 6e4) / 1000;

Putting it all together in one function:

function timeLeft() {    var now = new Date();    var endDate = new Date(Date.UTC(2017,4,29)); // 2017-05-29T00:00:00Z    var diff = endDate - now; 
var hours = Math.floor(diff / 3.6e6); var minutes = Math.floor((diff % 3.6e6) / 6e4); var seconds = Math.floor((diff % 6e4) / 1000); console.log('Time remaining to ' + endDate.toISOString() + ' or\n' + endDate.toString() + ' local is\n' + hours + ' hours, ' + minutes + ' minutes and ' + seconds + ' seconds');}
timeLeft()


Related Topics



Leave a reply



Submit