1

I am using full calendar and using the date from the eventclick

calEvent._start._i         returns: 2015-12-01 09:00:00

and I want to compare it to another date although it is this format

2015-12-01

I have used

var date = new Date(calendarDate);
date.setHours(0,0,0,0);

which returns an invalid date format

I have also used

var calDate = _longDateFormat(calendarDate, "dd-mm-yy");

which errored on the javascript

Nothing seems to work, including dateFormat.

Ideas?

Dale K
  • 25,246
  • 15
  • 42
  • 71
bananabreadbob
  • 369
  • 2
  • 10
  • 26

2 Answers2

3

I was facing the same problem and I didn't want to use specific formatting options because I wanted the date to be displayed in the correct local format. I ended up using a simple regex replace, maybe this approach will help you as well:

new Date().toLocaleString().replace(/\s\d{2}:\d{2}:\d{2,4}$/, ''))
Arreis
  • 43
  • 7
1

You could do something like this:

function checkWhetherCorrectDate(requestedDate, calendarDate) {
    var date1 = new Date(calendarDate.replace(/-/g,'/'));
    var date2 = new Date(requestedDate);
    date1.setHours(date2.getHours(), date2.getMinutes(), date2.getSeconds());
    return date1.getTime() === date2.getTime();
}


document.write(checkWhetherCorrectDate("2015-12-01", "2015-11-30 09:00:00") + '<br />')
document.write(checkWhetherCorrectDate("2015-12-01", "2015-12-01 09:00:00") + '<br />')
Pete
  • 57,112
  • 28
  • 117
  • 166
  • `function checkWhetherCorrectDate(requestedDate, calendarDate){ var date1 = new Date(calendarDate); var date2 = new Date(requestedDate); date1.setTime(date2.getTime()); if (date1 == date2){ return true; } return false; }` This for some reasons made the dates the same. So calendarDate went from 30 nov to 1st dec. – bananabreadbob Dec 01 '15 at 11:40
  • requestedDate = 2015-12-01 and the calendarDate = 2015-11-30 09:00:00 after `date1.setTime(date2.getTime())` date1 = Tue Dec 01 2015 00:00:00 – bananabreadbob Dec 01 '15 at 11:48
  • Ah ok, I see my error - setTime sets the time since january 1st 1970. See my edit. I have also changed your date comparing as it would always return false – Pete Dec 01 '15 at 11:55
  • I just tried your `setHours(0,0,0,0)` and it seems to work fine for me: http://jsfiddle.net/futww3e2/ – Pete Dec 01 '15 at 12:00
  • I can't understand why neither of these are doing anything. The solution you gave above formats date one as invalid. – bananabreadbob Dec 01 '15 at 12:05
  • It turns out your new Date(calendarDate) is what is failing - it was working for me as chrome is more flexible but ff and ie fail. See edit above, should now work in all browsers – Pete Dec 01 '15 at 12:33