Learn Simpli

Free Online Tutorial For Programmers, Contains a Solution For Question in Programming. Quizzes and Practice / Company / Test interview Questions.

Javascript difference between two dates in days

Javascript difference between two dates in days

In javascript, the difference between two dates in days can be calculated by subtracting the two dates. Before subtracting, the input values should be converted into the date.

Steps to calculate the difference between two dates in days:

1) Convert the input values into a date using the keyword new Date()
2) Subtract the dates dateOne – dateTwo
3) Divide the substracted value by milliseconds in a day (1000 * 60 * 60 * 24), 10)
4) Convert the result to an integer using the parseInt() or Math functions as shown in the below

Solution 1:
1) Define an arrow function differenceInDays that accepts the two parameters dateOne and dateTwo as an argument.
2) Convert the argument values to the dates by using new Date().
3) Subtract the two dates with Math.abs() function that returns an absolute value in time.
4) Return result by dividing the time by milliseconds in a day  (1000 * 60 * 60 * 24) with Math.ceil() function

var differenceInDays = (dateOne, dateTwo) => {
    var dateOne = new Date(dateOne);
    var dateTwo = new Date(dateTwo);
    var differenceInTime = Math.abs(dateTwo - dateOne);
    return Math.ceil(differenceInTime / (1000 * 60 * 60 * 24));
}

console.log(differenceInDays('12/13/2019', '12/15/2019'));
// output
// 2

Solution 2:
1) Define an arrow function differenceInDays that accepts the two parameters dateOne and dateTwo as an argument.
2) Convert the argument values into the dates by using new Date().
3) Subtract the two dates and divide by milliseconds in a day  (1000 * 60 * 60 * 24)
4) Return result by converting it to an integer with help of parseInt.

var differenceInDays = (dateOne, dateTwo) => {
    var dateOne = new Date(dateOne);
    var dateTwo = new Date(dateTwo);
    return parseInt((dateTwo - dateOne) / (1000 * 60 * 60 * 24), 10);
}
console.log(differenceInDays('12/13/2019', '12/15/2019'));
// output
// 2

Solution 3:
1) Define an arrow function differenceInDays that accepts the two parameters dateOne and dateTwo as an argument.
2) Convert the argument values into the dates by using new Date().
3) Use the UTC for getting year, month and day.
4) Subtract the two dates and divide by milliseconds in a day  (1000 * 60 * 60 * 24)
5) Return result by using Math.floor() methods

var differenceInDays = (dateOne, dateTwo) => {
    paramDateOne = new Date(dateOne);
    paramDateTwo = new Date(dateTwo);
    return Math.floor((Date.UTC(paramDateTwo.getFullYear(), paramDateTwo.getMonth(), paramDateTwo.getDate()) - Date.UTC(paramDateOne.getFullYear(), paramDateOne.getMonth(), paramDateOne.getDate())) / (1000 * 60 * 60 * 24));
}

console.log(differenceInDays('12/13/2019', '12/15/2019'));
// output
// 2

Solution 4:
1) Define an arrow function differenceInDays that accepts the two parameters dateOne and dateTwo as an argument.
2) Convert the argument values into the dates by using new Date().
3) Use the Date.getTime() method for substracting
4) Return result by dividing by milliseconds in a day  (1000 * 60 * 60 * 24)

var differenceInDays = (dateOne, dateTwo) => {
    var dateOne = new Date("12/13/2019");
    var dateTwo = new Date("12/15/2019");
    var differencInTime = dateTwo.getTime() - dateOne.getTime();
    return differencInTime / (1000 * 3600 * 24);
}

console.log(differenceInDays('12/13/2019', '12/15/2019'));
// output
// 2