Javascript 检查日期是否大于今天

Javascript 检查日期是否大于今天,javascript,jquery,Javascript,Jquery,尝试Jquery以确认所选日期是否等于今天或大于。 如果我选择今天,它将返回它,因为选择的日期少于今天。选择“前一天”效果很好,但选择“今天”返回的结果少于。任何小费 var firstRepaymentDate = new Date($('#First_Repayment_Date').val()); var today = new Date(); if (firstRepaymentDate.getTime() < tod

尝试Jquery以确认所选日期是否等于今天或大于。 如果我选择今天,它将返回它,因为选择的日期少于今天。选择“前一天”效果很好,但选择“今天”返回的结果少于。任何小费

var firstRepaymentDate = new Date($('#First_Repayment_Date').val());
                var today = new Date();
                if (firstRepaymentDate.getTime() < today.getTime()) {
                    alert('The First Repayment Date Can only Be Today Or Future Date');
                    return false;
                }
var firstreturnmentdate=新日期($('First\u returning\u Date').val();
var today=新日期();
如果(FirstReturnmentDate.getTime()
新日期()
不仅考虑日期,还考虑时间。我认为实现这一点的最简单方法是分别使用
getFullYear()、getMonth()、getDate()比较年、月和日。

在这里检查所有操作js Date的方法

新建日期()
不仅考虑日期,还考虑时间。我认为实现这一点的最简单方法是分别使用
getFullYear()、getMonth()、getDate()比较年、月和日。

在这里检查所有操作js Date的方法

不要忘记
新日期()
也将包括当前时间。您需要使用
today.setHours(0,0,0,0)
删除该时间组件,以便比较正确

另外,
setHours()
返回底层值,如
getTime()
,因此您可以执行以下操作

var firstreturnmentdate=新日期($('First\u returning\u Date').val();
var today=新日期();
if(firstreturnmentdate.getTime()
针对关于增加20天的评论:

这有点详细,但相当简单

var today=新日期();
var plus20Days=新日期(today.setDate(today.getDate()+20));

同样,您可以使用
setHours()
重置时间组件。

不要忘记
new Date()
也将包括当前时间。您需要使用
today.setHours(0,0,0,0)
删除该时间组件,以便比较正确

另外,
setHours()
返回底层值,如
getTime()
,因此您可以执行以下操作

var firstreturnmentdate=新日期($('First\u returning\u Date').val();
var today=新日期();
if(firstreturnmentdate.getTime()
针对关于增加20天的评论:

这有点详细,但相当简单

var today=新日期();
var plus20Days=新日期(today.setDate(today.getDate()+20));

同样,您可以使用
setHours()
重置时间组件。

new Date()
将包括当前时间,而不仅仅是日期。今天尝试
。设置小时数(0,0,0,0)
将时间设置为午夜。
new Date()
将包括当前时间,而不仅仅是日期。今天尝试
。设置小时数(0,0,0,0)
将时间设置为午夜。好主意,没有考虑“重置”时间hour@phuzi,谢谢你。如果我想检查所选日期是否比今天至少多20天,该怎么办?请参阅更新的答案好主意,没有考虑“重置”日期hour@phuzi,谢谢你。如果我想检查所选日期是否比今天至少多20天,该怎么办?请参阅更新的答案