Moment.js - 日期比较:捕捉最小范围 [英] Moment.js - Date comparison: Catching min range

查看:1555
本文介绍了Moment.js - 日期比较:捕捉最小范围的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的表单有一个Kendo DatePicker和一个按钮,在执行form.submit()之前运行JavaScript函数。我需要能够比较我输入的下限日期,并阻止代码继续(提交表单),如果它早于我的设定日期。如果限制没有被破坏,我想返回false,如果有的话。我将使用此示例来显示我在验证代码中所做的工作:

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();

我已经将1/1/0001输入到控件中,它不断地给出 lowerLimitBreached 为false,当它应该是真的,因为1-1-0001显然早于7-4-1776 ...我尝试使用 .isBefore( ),但遇到相同的问题,文档实际上说注意:moment()。isBefore()具有未定义的行为,不应该使用!,所以我不敢用。

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.

时刻文档: http://momentjs.com/docs/#/query/

推荐答案

重新使用moment.js,根本不要使用JavaScript Date对象。

Since you're using moment.js, don't use the JavaScript Date object at all. Stick to moment objects only.

//evaluates correctly
moment('0001-01-01');

//evaluates to Feb 2, 0001 - makes no sense...
new Date(Date.parse('0001-02-03'))
                            //^ but that's a 3, not a 2

//true - Jan 1, 0001 is before June 4, 1776
moment('0001-01-01').diff(moment('1776-06-04')) < 0

所以,看看你的代码,尝试一下:

So, looking at your code, try this out:

var lowerBound = moment('1776-06-04');
var dateField = $('#myDateField').val();

// String replace and put in proper format for moment
dateField = dateField.replace(/(\/)/g,'-');
var year = dateField.split('-')[2];
var month = dateField.split('-')[0];
var day = dateField.split('-')[1];

if (month < 10) { month = "0" + month; }
if (day < 10) { day = "0" + day; }
dateField = year + '-' + month + '-' + day;

// Enter into moment and compare
var dateToConsider = moment(dateField);
var lowerLimitBreached = dateToConsider.diff(lowerBound) < 0;

if (lowerLimitBreached)
    $('form').submit();

这篇关于Moment.js - 日期比较:捕捉最小范围的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆