我的表单有一个 Kendo DatePicker 和一个在执行 form.submit() 之前运行 JavaScript 函数的按钮。我需要能够将输入的日期与下限进行比较,如果代码早于我设定的日期,则阻止代码继续(提交表单)。如果没有违反限制,我想返回 false,如果有,则返回 true。我将使用这个示例来展示我在验证代码中要执行的操作:
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();
我一直在将 1/1/0001 输入到控件中lowerLimitBreached
,当它应该为真时,它一直给出错误,因为 1-1-0001 显然早于 7-4-1776 ...我尝试使用.isBefore()
,但是遇到了同样的问题,文档实际上说NOTE: moment().isBefore() has undefined behavior and should not be used!
,所以我不敢使用。
时刻文档: http: //momentjs.com/docs/#/query/