Two Methods for JavaScript to judge the input date

Source: Internet
Author: User

JavaScript code
/// Check whether the input date is in the correct date format:
/// Supports four input formats: yyyy-M-d, yyyy-MM-dd, yyyy/M/d, and yyyy/MM/dd.

Function checkDate (strInputDate ){
// Define a constant array of months and days
Var DA = [0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31];

// Unified Date Format
StrDate = strInputDate. replace (/-/g ,"/");

// Determine whether the date is in the expected format
If (strDate. indexOf ("/") =-1 ){
Alert ("Enter the format yyyy-M-d, yyyy-MM-dd, yyyy/M/d, and yyyy/MM/dd. ")
Return false;
}

// Year, month, and day
ArrD = strDate. split ("/");
If (arrD. length! = 3) return false;
Y = parseInt (arrD [0], 10 );
M = parseInt (arrD [1], 10 );
D = parseInt (arrD [2], 10 );

// Determine whether the value is a number.
If (isNaN (y) | isNaN (m) | isNaN (d) return false;

// Determine whether the month is between 1 and 12
If (m> 12 | m <1) return false;
// Determine whether it is a leap year
If (isLoopYear (y) DA [2] = 29;

// Determine whether the input date exceeds the total number of days of the month.
If (d> DA [m]) return false;

// If all the conditions are verified, it should be a valid date.
// If You Want to format the date once, you can process it here. The following format is the date format recognized by the database: yyyy-MM-dd.
// Str = y + "-" + (m <10? "0": "") + m + "-" + (d <10? "0": "") + d;
Str = y + "-" + (m <10? "0": "") + m + "-" + (d <10? "0": "") + d;
Alert (str)
Return true;
}
Function isLoopYear (theYear ){
Return (new Date (theYear, 1, 29). getDate () = 29 );
}

// Method 2:
/// Check whether the input date is in the correct date format:
/// Supports four input formats: yyyy-M-d, yyyy-MM-dd, yyyy/M/d, and yyyy/MM/dd.
Function CheckDate2 (strInputDate ){
If (strInputDate = "") return false;
StrInputDate = strInputDate. replace (/-/g ,"/");
Var d = new Date (strInputDate );
If (isNaN (d) return false;
Var arr = strInputDate. split ("/");
Return (parseInt (arr [0], 10) = d. getFullYear () & (parseInt (arr [1], 10) = (d. getMonth () + 1) & (parseInt (arr [2], 10) = d. getDate ()));
}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.