如何比较当前日期之前和之后的两个日期

时间:2015-10-19 10:58:45

标签: javascript jquery date fullcalendar

我在一个应用程序中工作,我将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')
    }
} 

此代码无效,无法检查日期,即日历的日期或日历的提前日期。请有人帮忙。

2 个答案:

答案 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')
        }
相关问题