JavaScript detects date time format is correct code

Source: Internet
Author: User
Tags getdate

In web development, often with datetime, sometimes we need users to provide date and time data, in order to ensure the smooth submission, we need to validate the user input date and time format, the user will be prompted to check and re-enter, the following code to determine the date format, Date time format is correct.
First: Only determine if the date format is correct and does not take time:

The code is as follows Copy Code

<script language=javascript>
String.prototype.isDate = function ()
{
var r = This.match (/^ (\d{1,4}) (-|\/) (\d{1,2}) \2 (\d{1,2}) $/);
if (r==null) return false; var d = new Date (r[1], r[3]-1, r[4]);
Return (D.getfullyear () ==r[1]&& (D.getmonth () +1) ==r[3]&&d.getdate () ==r[4]);
}
Alert ("2002-01-31". IsDate ());
Alert ("2002-01-41". IsDate ());
</script>

the second is to determine whether the date + time format is correct:
 

The code is as follows Copy Code

<script language=javascript>
String.prototype.isTime = function ()
{
var r = This.match (/^ (\d{1,4}) (-|\/) (\d{1,2}) \2 (\d{1,2}) (\d{1,2}):(\d{1,2}):(\d{1,2);
if (r==null) return false; var d = new Date (r[1], r[3]-1,r[4],r[5],r[6],r[7]);
Return (D.getfullyear () ==r[1]&& (D.getmonth () +1) ==r[3]&&d.getdate () ==r[4]&&d.gethours () = =r[5]&&d.getminutes () ==r[6]&&d.getseconds () ==r[7]);
}
Alert ("2002-1-31 12:34:56". Istime ());
Alert ("2001-2-29 12:54:56". Istime ());
Alert ("2002-1-41 12:00:00". Istime ());
</script>

the third is to determine whether the date is valid


1 short time, form (13:04:06)

The code is as follows Copy Code
function Istime (str)
{
var a = Str.match (/^ (\d{1,2}) (:)? \d{1,2}) \2 (\d{1,2}) $/);
if (a = = null) {alert (' input parameter is not a time format '); return false;}
if (a[1]>24 | | a[3]>60 | | a[4]>60)
{
Alert ("Time format is not");
return False
}
return true;
}
2. Short date, form (2008-07-22)
function Strdatetime (str)
{
var r = Str.match (/^ (\d{1,4}) (-|\/) (\d{1,2}) \2 (\d{1,2}) $/);
if (r==null) return false;
var d= new Date (r[1], r[3]-1, r[4]);
Return (D.getfullyear () ==r[1]&& (D.getmonth () +1) ==r[3]&&d.getdate () ==r[4]);
}
3 long time, shaped like (2008-07-22 13:04:06)
function Strdatetime (str)
{
var reg =/^ (\d{1,4}) (-|\/) (\d{1,2}) \2 (\d{1,2}) (\d{1,2}):(\d{1,2}):(\d{1,2}) $/;
var r = Str.match (reg);
if (r==null) return false;
var d= new Date (r[1], r[3]-1,r[4],r[5],r[6],r[7]);
Return (D.getfullyear () ==r[1]&& (D.getmonth () +1) ==r[3]&&d.getdate () ==r[4]&&d.gethours () = =r[5]&&d.getminutes () ==r[6]&&d.getseconds () ==r[7]);
}

the fourth is to determine whether the date format is valid

  code is as follows copy code

function todatefromstring (strdate)
{
if (strdate.length!= 8) {
RET Urn null;
}
var dtdate = null;
var nyear = parseint (strdate.substring (0, 4);
var nmonth = parseint (strd Ate.substring (4, 6), 10);
var nday = parseint (strdate.substring (6, 8), 10);
if (isNaN (nyear) = = True | | isNaN (nmonth) = = True | | isNaN (nday) = = True)
{
return null;
}
Dtdate = new Date (nyear, nMonth-1, nday);
if (nyear!= dtdate.getfullyear () | | (nMonth-1)!= dtdate.getmonth () | | Nday!= dtdate.getdate ())
{
return null;
}
return dtdate;
}
YYYYMMDD format

Related Article

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.