My form has a Kendo DatePicker and a button that runs a JavaScript function prior to doing a form.submit(). I need to be able to compare my date that is entered against a lower limit, and prevent the code from going on (submitting the form) if it is earlier than my set date. I want to return false if the limit hasn't been breached, true if it has. I'll use this example to show what I'm trying to do in the validation code:
var lowerBound = new Date('1776', '6','4'); // July 4, 1776
var dateField = $('#myDateField').val(); // <-- comes from Kendo DatePicker
var dateToConsider = new Date(dateField); // if this is 1/1/0001, this changes to 1/1/1901... not good, so I do this, instead....
var arrDate = dateField.split('/'); // I also check for '-', ' ', and '.'
dateToConsider = arrDate[2] + '-' + arrDate[0] + '-' arrDate[1]; // could add 'T00:00:00Z'; but don't think it matters
var momentDTC = moment(dateToConsider);
var lowerLimitBreached = moment(lowerBound).isAfter(momentDTC); // <-- this is always false
if (!lowerLimitBreached)
$('form').submit();
I've been inputting 1/1/0001 into the control, and it keeps giving lowerLimitBreached
as false, when it should be true, since 1-1-0001 is clearly earlier than 7-4-1776... I tried using .isBefore()
, but met with the same issue, and the documentation actually says NOTE: moment().isBefore() has undefined behavior and should not be used!
, so I dare not use that.
Moment documentation: http://momentjs.com/docs/#/query/