我正在一个应用程序中工作,我正在比较从完整日历中选择的日期与当前日期,我需要检查所选日期小于当前日期或提前当前日期。我所做的我张贴在这里
var selectedDate = $('#selectedDate').val();
var today = new Date();
var str_date = selectedDate.split(',');
for(var i =0 ;i<str_date.length;i++){
str_date[i] = str_date[i].replace(/^s*/, "").replace(/s*$/, "");
if(str_date[i] <=today){
alert('before date')
}
}
这个代码不工作,它不能检查日期是过期日期还是日历中的提前日期。
从fullCalendar
获取日期,并将new Date()
的小时、分钟、秒和毫秒设置为0,并创建第二天日期,然后比较日期。
检查下面的代码片段:
var selectedDate = "2015-10-20"; // Replace with $('#selectedDate').val();
var today = new Date();
var selectedDate = new Date(Date.parse(selectedDate));
today.setHours(0);
today.setMinutes(0);
today.setSeconds(0);
today.setMilliseconds(0);
var nextDay = new Date(today.getTime());
nextDay.setDate(nextDay.getDate() + 1);
if(selectedDate < today) {
snippet.log('Date is before');
} else if(selectedDate >= today && selectedDate < nextDay) {
snippet.log('Date is today');
} else {
snippet.log('Date is after');
}
<script src="http://tjcrowder.github.io/simple-snippets-console/snippet.js"></script>
看看
var selectedDate = $('#selectedDate').fullCalendar('getDate').format();
var currentDate = new Date();
if(new Date(selectedDate) <= currentDate ){
alert('before date')
}