Question

i am trying to sum up or deduct dates in selected range using jquery datepicker, here is how i am doing it right now but without any luck.

function test(){
    var sundayCheck = 0;
    var saturdayCheck = 0;
    var totalDays = 0;

    sundayCheck = new Date($("#onlySunday").datepicker("getDate"));
    saturdayCheck = new Date($("#onlySaturday").datepicker("getDate"));

    totalDays = saturdayCheck.getDate() - sundayCheck.getDate();

    alert(totalDays);
}

thats a piece of code that should make calculation but what it does is just deducts days like,

24.01.10 - 06.02.10 = -18 which is totaly wrong as it should be more days its just does not consider the days and its just using first numbers of dates.

If anyone could help how to make such calculations i would appreciate the most.

Was it helpful?

Solution

Are you trying to dtermine the number of days between two dates?

  totalDays = Math.ceil((saturdayCheck.getTime()-sundayCheck.getTime())/(1000*60*60*24);

First get the number of milliseconds between the two dates then divide by the number of milliseconds in a day. Using Math.ceil to round up to nearest integer.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top