这是检查JavaScript中有效日期的好方法吗?

问题描述:

由于我不是JavaScript专家,请更正或解释我的过度简化是不正确的.

Please correct or explain how my over-simplification is incorrect as I am not a JavaScript expert.

但是我只需要知道对象是否是有效日期即可.这只会来自用户输入(即文本框).

But I just need to know if an object is a valid date. This will only come from user input (ie, text box).

var is_valid_date = function(date) {
    try {
        var d = new Date(date);
        return true;
    }
    catch(e) {
        return false;
    }
}

您必须决定要接受哪种日期形式.

YOU have to decide what form of dates you want to accept.

然后,一旦知道要接受的格式,就可以检查新Date(str) date.parse() 在MDN上,看看它是否完全支持您想要的内容,是否支持错误条件下正确的事情(可能不会).如果没有,那么您将必须进行一些手动解析.

Then, once you know what forms you want to accept, you can then check the spec for new Date(str) or date.parse() on MDN and see if it supports exactly what you want and if it does the right things on error conditions (it probably will not). If not, then you will have to do some manual parsing.

如果您需要我们的进一步帮助,则需要指定要接受的日期格式.

If you want further help from us, you will need to specify what forms of date you want to accept.

浏览器也存在一些差异,因为javascript已移动到支持其他日期格式的位置,并且较早的浏览器之间存在一些不一致,这都意味着您将需要使用一系列合法和非法的日期格式字符串来构建自己的简单测试脚本并查看您的有效性检测是否可以在多个浏览器中实现您想要的功能.这不是正确的火箭科学,但也不是一件容易的事,除非您只想接受原始日期对象支持的内容(这不太可能),否则就需要做一些工作.

There are also some browser differences as javascript has moved to support additional date formats and earlier browsers had some inconstencies between them which all means you'll want to build yourself a simple test script with a bunch of legal and illegal date format strings and see if your validity detection does what you want in several browsers. This isn't rocket science to get it right, but it's not trivial either and requires some work unless you only want to accept what the original date object supported (which is unlikely).

如果这是我的代码,我可能会决定对所需的输入格式进行手动解析的工作量要少得多,因为您知道这种输入格式是100%的确定性,因为它是您自己的手动解析,因此可以在所有浏览器中使用.我可能会使用正则表达式来解析日期,然后将每个组件转换为数字并检查每个组件的有效性.然后,您可以将这些数字分量提供给Date构造函数以创建Date对象.

If this were my code, I'd probably decide that it's far less work to do manual parsing of your desired input format that you know with 100% certainty will work in all browsers because it's your own manual parsing. I'd probably use a regex to parse the date and then convert each component to a number and check each component for validity. You can then feed those numeric components to the Date constructor to create the Date object.

如果您现在可以确定,则内置日期类对于用户输入的输入不是很有用.如果您愿意为此使用库,则 date.js库在这方面.

If you can tell by now, the built-in date class isn't very useful for user entered input. If you're willing to use a library for this, the date.js library has a ton of useful functionality in this regard.

以下是接受以下美国格式的手动解析功能的示例:

Here's an example of a manual parsing function that accepts these US formats:

mm-dd-yyyy
mm dd yyyy
mm/dd/yyyy

JS代码:

function checkDate(str) {
    var matches = str.match(/(\d{1,2})[- \/](\d{1,2})[- \/](\d{4})/);
    if (!matches) return;

    // parse each piece and see if it makes a valid date object
    var month = parseInt(matches[1], 10);
    var day = parseInt(matches[2], 10);
    var year = parseInt(matches[3], 10);
    var date = new Date(year, month - 1, day);
    if (!date || !date.getTime()) return;

    // make sure we have no funny rollovers that the date object sometimes accepts
    // month > 12, day > what's allowed for the month
    if (date.getMonth() + 1 != month ||
        date.getFullYear() != year ||
        date.getDate() != day) {
            return;
        }
    return(date);
}

以及具有一些测试用例的演示: http://jsfiddle.net/jfriend00/xZmBY/

And a demo with some test cases: http://jsfiddle.net/jfriend00/xZmBY/

如果您想要欧元格式,将代码切换为该格式很简单.无论哪种情况,您都必须决定接受哪种格式,为其编码,然后与用户沟通所需的格式.如果您认为这很麻烦,那么也许您会明白为什么这么多的网站使用的日期日历选择器没有这种复杂性.

If you want the Euro format, it's a trivial matter to switch the code to that. In either case, you have to decide which format you accept, code for it and then communicate to the user which format is required. If you think this is messy, then perhaps you will see why so many sites use a date calendar picker that doesn't have this complexity.