我在一个应用程序中工作,我将fullcalendar中的选定日期与当前日期进行比较,我需要检查所选日期是否小于当前日期或提前当前日期。我在这里发布了什么
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')
}
}
此代码无效,无法检查日期,即日历的日期或日历的提前日期。请有人帮忙。
答案 0 :(得分:0)
从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>
答案 1 :(得分:-1)
检查出来
var selectedDate = $('#selectedDate').fullCalendar('getDate').format();
var currentDate = new Date();
if(new Date(selectedDate) <= currentDate ){
alert('before date')
}