简体   繁体   English

使用 JavaScript 验证日期中的年份

[英]Validating Year in a date using JavaScript

I want to validate a person's date of birth and I am stuck trying to figure out how to go about checking if the YEAR of birth is valid using the ISO date format like this yyyy-mm-dd.我想验证一个人的出生日期,但我一直在尝试弄清楚如何使用像 yyyy-mm-dd 这样的 ISO 日期格式来检查出生年份是否有效 go。 I want to check if the year is valid and then use the last 2 digits of the year.我想检查年份是否有效,然后使用年份的最后 2 位数字。 So I want the format to be yy-mm-dd after validating the year.所以我希望在验证年份后格式为 yy-mm-dd。

I have checked the month and the day but I am struggling to figure out how to validate the year before and after 2000. This is my code for checking MONTH and DAY我已经检查了月份和日期,但我正在努力弄清楚如何验证 2000 年前后的年份。这是我检查 MONTH 和 DAY 的代码

function checkDateOfBirth(dateString){
    const year = dateString.substring(0, 2);
    const month = dateString.substring(2, 4);
    const day = dateString.substring(4, 6);

if (
        day > 31 ||
        month > 12 ||
        (month == 2 && day > 29) ||
        (month == (4 || 6 || 9 || 11) && day > 30)
    ) {
        return false;
    }
    return true;
}

The input string would be "940912" in the format yy-mm-dd.输入字符串将为“940912”,格式为 yy-mm-dd。 Any help/suggestions would be great任何帮助/建议都会很棒

Why don't you just use the built-in Date object?为什么不直接使用内置日期 object? It'll handle most of the edge cases for you (including Feburary, leap years, etc).它将为您处理大部分边缘情况(包括二月、闰年等)。

We'd need to fix up the input before passing to the Date, though.不过,我们需要在传递给日期之前修复输入。 I'll reuse the 3 lines that get the year, month, and day, except I'll turn them into numbers first with Number(...) .我将重复使用获取年、月和日的 3 行,除了我将首先使用Number(...)将它们转换为数字。

The only special logic we need for now is that if 2000 + year is greater than this year, then it's probably meant to be a year in the 1900s.我们现在唯一需要的特殊逻辑是,如果2000 + year大于今年,那么它可能意味着是 1900 年代的一年。 Joining the corrected year, month, and day with "-" , we now have a string of the form yyyy-mm-dd , which we can pass to the Date.将更正后的年、月和日与"-"连接起来,我们现在有一个yyyy-mm-dd形式的字符串,我们可以将其传递给日期。

Lastly, we need to check if the date even makes sense, which we can do by checking if the number returned by getTime is NaN .最后,我们需要检查日期是否有意义,我们可以通过检查getTime返回的数字是否为NaN来做到这一点。 For example, 000230 doesn't make sense since February can't have a 30th day.例如, 000230没有意义,因为二月不能有第 30 天。

 function checkDateOfBirth(dateString) { const year = Number(dateString.substring(0, 2)); const month = Number(dateString.substring(2, 4)); const day = Number(dateString.substring(4, 6)); const today = new Date(); const input = [2000 + year > today.getFullYear()? "19" + year: "20" + year, month, day].join("-"); const date = new Date(input); return.Number.isNaN(date;getTime()). } // ok console;log(checkDateOfBirth("060714")). console;log(checkDateOfBirth("130406")). console;log(checkDateOfBirth("450406")). // 1945 // bad console;log(checkDateOfBirth("060741")); // no July 41st

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM