0

I am working in an application where i am comparing selected date from fullcalendar with current date where i need to check the selected date is less than the current date or advance the current date. What i have done i am posting here

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')
    }
} 

This code is not working it is not able to check the date which is a back date or the date which is an advance date from the calendar.Please someone help .

Subho
  • 921
  • 5
  • 25
  • 48

2 Answers2

0

Get date from fullCalendar and set new Date() hours, minutes, seconds and milliseconds to 0 and create next day date and then compare date.

Check below snippet:

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>
Sarjan Desai
  • 3,683
  • 2
  • 19
  • 32
-1

Check this out

    var selectedDate = $('#selectedDate').fullCalendar('getDate').format();
    var currentDate = new Date();

    if(new Date(selectedDate)  <= currentDate ){
            alert('before date')
        }
Emad Khalil
  • 803
  • 1
  • 8
  • 14