5

the 1 st one which get using the id has format

var checkindate = $('#check-in').text();

28-07-2011

then i get the current date using

var now = new Date();

and it has the format

Wed Jul 20 2011 19:09:46 GMT+0530 (IST)

i want to get the date difference from these two dates . in this case it is 8 . i searched a lot and could not find an answer , please help....... :'(

Curtis
  • 101,612
  • 66
  • 270
  • 352
Kanishka Panamaldeniya
  • 17,302
  • 31
  • 123
  • 193

2 Answers2

5

You can parse the initial date, feed it to a date object, substract it with the current time to get a millisecond difference, and then divide it by the number of milliseconds in a day.

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

At the time of writing this gives -7.5. It's a negative number because the date is in the future. If you want a positive number, just swap the variables in the substraction. If you want a round number, just use Math.round.

Alex Turpin
  • 46,743
  • 23
  • 113
  • 145
3

You can parse your first date as described in this thread: Parse DateTime string in JavaScript

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

var date = new Date(dateParts[2], (dateParts[1] - 1) ,dateParts[0]);

And then compare the two dates as asked here: Compare two dates with JavaScript

Community
  • 1
  • 1
JMax
  • 26,109
  • 12
  • 69
  • 88