JavaScript day difference function outputs wrong days - javascript

I use the following JS function to calculate the difference of days between today and a day in the future:
var oneDay = 24 * 60 * 60 * 1000;
var today = new Date();
var futureDay = new Date(futureDate);
var diffDays = Math.round(Math.abs((today.getTime() - futureDay.getTime()) / (oneDay)));
My problem: When the futureDate is today, I get the result "1" and if it is tomorrow, I get "0".
What is wrong about this function?

Try this way, you didn't set future date
var oneDay=1000 * 3600 * 24;
var dateToday = new Date("02/24/2015"); //or just, new Date();
var dateFuture = new Date("02/25/2015"); // set here future date like this
var timeDiff = Math.abs(dateFuture.getTime() - dateToday.getTime());
var diffDays = Math.ceil(timeDiff /oneDay);
alert(diffDays);

Related

How to find the difference between two dates?

Hi Guys i have two date
firstDate = "06/11/2019"
lastDate = "15/11/2019"
var date1 = new Date(firstDate);
var date2 = new Date(lastDate);
var diffTime = Math.abs(date2 - date1);
var diffDays = Math.ceil(diffTime / (1000 * 60 * 60 * 24));
i get diffDays=NaN. how can i do this. Thanks.
Your lastDate is an invalid date.
You can check this with
Date.parse("15/11/2019 00:00:00")
which will return a NaN
Check the Warnings section
https://www.w3schools.com/js/js_date_formats.asp

Jquery Date difference in Hours

I am trying to find date difference between 2 dates in hours with out using any 3rd party dates related library. I used the below code but it shows wrong hours. Any better suggestion or pointer to correct this please.
My fiddle
var FutureDate=new Date('2016-05-08T05:19:05.83');
var TodayDate = new Date();
var t1 = FutureDate.getTime();
var t2 = TodayDate.getTime();
var diffInHours = parseInt((t2-t1)/(24*3600*1000));
alert(diffInHours);
var futureDate = new Date('2016-06-08T05:19:05.83');
var todayDate = new Date();
var milliseconds = futureDate.getTime() - todayDate.getTime();
var hours = Math.floor(milliseconds / (60 * 60 * 1000));
alert('Hours: ' + hours);
hour : minute = 1 : 60
minute : second = 1 : 60
second : millisecond = 1 : 1000
hour: millisecond = 1 : 60x60x1000
You can actually substract dates from each other natively.
var msIn1Hour = 3600 * 1000;
var TodayDate = new Date();
var FutureDate = new Date('2016-05-08T05:19:05.83');
alert((TodayDate - FutureDate)/msIn1Hour);
Note: I'm not quite sure why you used parseInt as this is meant to be used to convert a string into a int. If you want to round a number, use Math.floor or Math.round.
Just remove the divide by 24 from your formula. That 24 is actually converting it to a difference in days.
t2-t1 = x milliseconds
x/1000 = y seconds
y/3600 = z hours
You are done when you have z. So, you dont have to do the divide by 24. So, you can simply write
var FutureDate=new Date('2016-05-08T05:19:05.83');
var TodayDate = new Date();
var t1 = FutureDate.getTime();
var t2 = TodayDate.getTime();
var diffInHours = Math.floor((t2-t1)/(3600*1000)); //Removed the 24 here
alert(diffInHours);

Return minutes from date difference [duplicate]

This question already has answers here:
JavaScript - Get minutes between two dates
(12 answers)
Closed 7 years ago.
I have this function:
dateDifference: function(start_date, end_date)
{
var date1 = new Date(start_date);
var date2 = new Date(end_date);
var timeDiff = Math.abs(date2.getTime() - date1.getTime());
return timeDiff;
}
how you can see I calculate the difference between two dates passed as parameter, now the end result is like this:
55000
But I want the result in minutes how I can achieve this?
You got milliseconds so you can divide them by 1000 and 60 and get result in minutes.
dateDifference: function(start_date, end_date)
{
var date1 = new Date(start_date);
var date2 = new Date(end_date);
var timeDiff = Math.abs((date2.getTime() - date1.getTime()) / 1000 / 60);
return timeDiff;
}
to get from 55000 to seconds, divide by 1000.
then divide by 60 to get minutes.
like so:-
function dateDifference(start_date, end_date)
{
var date1 = new Date(start_date);
var date2 = new Date(end_date);
var milSeconds = Math.abs(date2.getTime() - date1.getTime());
var seconds = milSeconds / 1000;
var minutes = seconds / 60;
return minutes;
}
console.log(dateDifference('01/12/2016 09:00:00', '01/12/2016 10:00:00')); // 60 minutes

Comparing the difference between two dates in the US Date Standard of mm/dd/yyyy

I need your help,
How can one compare the difference between two given dates while using the US date standard of mm/dd/yyyy?
A few examples below:
11/20/2014 minus 11/25/2014 = -5
11/25/2014 minus 11/25/2014 = 0
11/27/2014 minus 11/25/2014 = 2
var date1 = new Date("11/20/2014");
var date2 = new Date("11/25/2014");
var timeDiff = Math.abs(date2.getTime() - date1.getTime());
var diffDays = Math.ceil(timeDiff / (1000 * 3600 * 24));
try this

How to calculate the difference between date of birth and the system date in JavaScript

I want to calculate the difference between a date of birth and the system date in JavaScript.
in JSP and JavaScript:
'Date Of Birth
'
function currentAge(DateOfBirth){
//alert(DateOfBirth);
var date1 = DateOfBirth;
date1 = document.getElementById("dateOfBirth").value;
alert(date1);
var date2 = new Date();
alert(date2);
// _Diff=Math.ceil((date2.getTime()-date1.getTime())/(one_day));
/* var timeDiff = Math.abs(date2.getTime() - date1.getTime());
var diffDays = Math.ceil(timeDiff / (1000 * 3600 * 24));
//var diffDays = date2.getDate() - date1.getDate(); */
//alert(_Diff);
}
function dateDiff(dateform) {
date1 = new Date();
date2 = new Date();
diff = new Date();
date1temp = new Date(dateform.firstdate.value);
date1.setTime(date1temp.getTime());
date2temp = new Date(dateform.seconddate.value);
date2.setTime(date2temp.getTime());
diff.setTime(Math.abs(date1.getTime() - date2.getTime()));
timediff = diff.getTime();
days = Math.floor(timediff / (1000 * 60 * 60 * 24));
dateform.difference.value = days;
return false;
}
Now its fine
function currentAge(DateOfBirth){
var date1 = document.getElementById("dateOfBirth").value;
alert(date1);
var date11=new Date(date1);
alert(date11);
var date2 = new Date();
alert(date2);
alert(3);
var timeDiff = Math.abs(date2.getYear() - date11.getYear());
alert(timeDiff);
}

Categories

Resources