如何在jquery中比较两个日期 [英] how to compare two dates in jquery
问题描述
获得使用id的第一个格式
the 1 st one which get using the id has format
var checkindate = $('#check-in').text();
28-07-2011
然后我使用<获取当前日期/ p>
then i get the current date using
var now = new Date();
它的格式为
Wed Jul 20 2011 19:09:46 GMT+0530 (IST)
<我希望从这两个日期得到日期差异。在这种情况下,它是8
。我搜索了很多,找不到答案,请帮助.......:'(
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);
在撰写本文时,这给出-7.5。这是一个负数,因为日期是将来。如果你想要一个正数,只需交换减法中的变量。如果你想要一个整数,只需使用Math.round。
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.
这篇关于如何在jquery中比较两个日期的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!