4

Please help me get the number of days between today's date and some other date.. Here is my example

It gives me NaN

Here is what I came up with. My demo

var cellvalue="2011-08-18 11:49:01.0 IST";
var firstDate = new Date();
var secondDate = cellvalue.substring(0, cellvalue.length-4);

alert(diffOf2Dates(firstDate,secondDate));

function diffOf2Dates(todaysDate,configDate)
{
/*var udate="2011-08-18 11:49:01.0";
var configDate=new Date(udate);*/

var oneDay = 24*60*60*1000; // hours*minutes*seconds*milliseconds
var firstDate = todaysDate; // Todays date
var secondDate = new Date(configDate);

var diffDays = Math.abs((firstDate.getTime() - secondDate.getTime())/(oneDay));
console.info(firstDate+", "+secondDate);
//console.info(Math.ceil(diffDays));

return Math.ceil(diffDays);
}
AabinGunz
  • 12,109
  • 54
  • 146
  • 218

6 Answers6

6

Use

var firstDate = new Date(); // Todays date
var secondDate = new Date(2011,08,19, 11,49,01);

var diffDays = (firstDate.getDate() - secondDate.getDate());

It was showing NAN as your constructor is wrong. check yourself by alerting secondDate in your original code

Edit : above code will work if both dates are in same month, for general case

var oneDay  = 24*60*60*1000;
var diffDays = Math.abs((firstDate.getTime() - secondDate.getTime()) / oneDay);

Also this will give result as fraction of date, so if you want to count whole dates you can use Math.ceil or Math.floor

Pradeep
  • 1,254
  • 1
  • 22
  • 41
  • @Pradeep: [This Example](http://jsfiddle.net/vQnHz/5/) does not work in IE, but works in all other browsers can you take a look. Thanks – AabinGunz Aug 19 '11 at 10:43
3

Use this:

var udate="2011-08-19 11:49:01.0 GMT+0530";

The IST part is not valid

Tejo
  • 547
  • 4
  • 15
3

your input date is incorrect that is why it is failing. anyways here is some code that should help you with it.

var DateDiff = {

    inDays: function(d1, d2) {
        var t2 = d2.getTime();
        var t1 = d1.getTime();

        return parseInt((t2-t1)/(24*3600*1000));
    },

    inWeeks: function(d1, d2) {
        var t2 = d2.getTime();
        var t1 = d1.getTime();

        return parseInt((t2-t1)/(24*3600*1000*7));
    },

    inMonths: function(d1, d2) {
        var d1Y = d1.getFullYear();
        var d2Y = d2.getFullYear();
        var d1M = d1.getMonth();
        var d2M = d2.getMonth();

        return (d2M+12*d2Y)-(d1M+12*d1Y);
    },

    inYears: function(d1, d2) {
        return d2.getFullYear()-d1.getFullYear();
    }
}
var udate="2011-08-05 11:49:01";
var configDate=new Date(udate);

var oneDay = 24*60*60*1000; // hours*minutes*seconds*milliseconds
var firstDate = new Date(); // Todays date
var secondDate = new Date(udate);

alert(secondDate);

var diffDays = DateDiff .inDays(firstDate,secondDate);

alert(diffDays );
Baz1nga
  • 15,485
  • 3
  • 35
  • 61
  • Thanks for your answer. Also [this Example of mine](http://jsfiddle.net/vQnHz/5/) does not work in IE, but works in all other browsers can you take a look. Thanks – AabinGunz Aug 19 '11 at 10:44
  • You can answer [here](http://stackoverflow.com/questions/7120485/continuation-of-count-difference-between-two-numbers) – AabinGunz Aug 19 '11 at 10:47
  • the problem is the fomat of the date: ie doesnt support the way other browsers do.. run this: http://jsfiddle.net/vQnHz/7/ – Baz1nga Aug 19 '11 at 10:50
2

if you have udate format like 28-07-2011 you can use this

var checkindatestr = "28-07-2011";
var dateParts = checkindatestr.split("-");

var checkindate = new Date(dateParts[2], dateParts[1] - 1, dateParts[0]);
var now = new Date();
var difference = now - checkindate;
var days = difference / (1000*60*60*24);

alert(days);

how to compare two dates in jquery

Community
  • 1
  • 1
Kanishka Panamaldeniya
  • 17,302
  • 31
  • 123
  • 193
1

You are calculating the difference correctly but the problem is that secondDate is an invalid date. Date cannot work with that date format, it needs "August 08, 2011 11:49:01" as input - and if your date has a different format then you have to convert it. Note that Date has only rudimentary timezone recognition, you can only be sure that "UTC" or "GMT" will be recognized correctly - you shouldn't use other time zones.

Wladimir Palant
  • 56,865
  • 12
  • 98
  • 126
1

The problem is with your udate variable value. The date format is not correct. Try initializing the date in this format:

var secondDate = new Date(year,month,date);
Sang Suantak
  • 5,213
  • 1
  • 27
  • 46