JS Date Format Judge code

Source: Internet
Author: User
Tags getdate

/**
Determine the date format entered in the input box is YYYY-MM-DD and the correct date
*/
function IsDate (sm,mystring) {
var reg =/^ (d{4})-(D{2})-(d{2}) $/;
var str = mystring;
var arr = reg.exec (str);
if (str== "") return true;
if (!reg.test (str) &&regexp.$2<=12&&regexp.$3<=31) {
Alert ("Please ensure that the date format entered in +sm+ is YYYY-MM-DD or the correct date!");
return false;
}
return true;
}


Method Two


//---------------------------------------------------------------------------
Determine if the date format is correct
The return value is an error message and no error message indicates a valid date string
function isdatestring (strdate) {
var strseparator = "-"; Date Separator
var Strdatearray;
var intyear;
var intmonth;
var intday;
var boolleapyear;
var errormsg = ""; Error message
Strdatearray = Strdate.split (strseparator);
No judgment length, in fact 2008-8-8 is also reasonable//strdate.length!= 10 | |
if (strdatearray.length!= 3) {
ErrorMsg + = "date format must be: YYYY-MM-DD";
return errormsg;
}
Intyear = parseint (strdatearray[0],10);
Intmonth = parseint (strdatearray[1],10);
Intday = parseint (strdatearray[2],10);
if (isNaN (intyear) | | isNaN (intmonth) | | isNaN (Intday)) {
ErrorMsg + = "date format error: Year and month date must be a pure number";
return errormsg;
}
if (intmonth>12 | | intmonth<1) {
ErrorMsg + = "date format error: Month must be between 1 and 12";
return errormsg;
}
if (intmonth==1| | intmonth==3| | intmonth==5| | Intmonth==7
|| intmonth==8| |intmonth==10| | INTMONTH==12)
&& (intday>31| | intday<1)) {
ErrorMsg = = "date format error: Semi-rotary must be between 1 and 31 days";
return errormsg;
}
if (intmonth==4| | intmonth==6| | intmonth==9| | INTMONTH==11)
&& (intday>30| | intday<1)) {
ErrorMsg + = "Date format error: The number of days must be between 1 to 31";
return errormsg;
}
if (intmonth==2) {
if (Intday < 1) {
ErrorMsg + = "date format error: Date must be greater than or equal to 1";
return errormsg;
}
Boolleapyear = false;
if ((intyear%100) = = 0) {
if ((intyear%400) = = 0)
Boolleapyear = true;
}
else{
if ((intyear% 4) = = 0)
Boolleapyear = true;
}
if (boolleapyear) {
if (Intday > 29) {
ErrorMsg + = "date format error: Leap year of February days can not exceed 29";
return errormsg;
}
} else {
if (Intday > 28) {
ErrorMsg + = "date format error: Non-leap year February days cannot exceed 28";
return errormsg;
}
}
}
return errormsg;
}

Method Three

Function Name: Checkdatetime
Feature Description: Check whether the date time
function Checkdatetime (str) {
var reg =/^ (d+)-(d{1,2})-(d{1,2}) (d{1,2}):(d{1,2}):(d{1,2}) $/;
var r = Str.match (reg);
if (r==null) return false;
R[2]=r[2]-1;
var d= new Date (r[1], r[2],r[3], r[4],r[5], r[6]);
if (D.getfullyear ()!=r[1]) return false;
if (D.getmonth ()!=r[2]) return false;
if (D.getdate ()!=r[3]) return false;
if (D.gethours ()!=r[4]) return false;
if (D.getminutes ()!=r[5]) return false;
if (D.getseconds ()!=r[6]) return false;
return true;
}

Other

String.prototype.isvalidtime=function ()
{
var resule=this.match (/^ (d{1,2}) (:)? d{1,2}) 2 (d{1,2}) $/);
if (result==null) return false;
if (result[1]>24 | | result[3]>60 | | result[4]>60) return FALSE;
return true;
} string.prototype.isvaliddate=function ()
{
var result=this.match (/^ (d{1,4}) (-|/) (d{1,2}) 2 (d{1,2}) $/);
if (result==null) return false;
var d=new Date (Result[1], result[3]-1, result[4]);
Return (D.getfullyear () ==result[1]&&d.getmonth () +1==result[3]&&d.getdate () ==result[4]);
}

For more details please see: http://www.111cn.net/wy/99/33223.htm

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.