开发者

how to compare two dates in jquery

开发者 https://www.devze.com 2023-03-21 22:23 出处:网络
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

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 开发者_StackOverflow社区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....... :'(


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.


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

0

精彩评论

暂无评论...
验证码 换一张
取 消