JS to determine whether the date format is valid simple example _javascript skills

Source: Internet
Author: User
Tags getdate

The

is similar to the checkdate in PHP.

Function Name: Checkdatetime//Feature Description: Check for 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;  /** 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 +sm+"
The date format entered in is YYYY-MM-DD or the correct date! ");
return false;
return true; } function todatefromstring (strdate) {if (Strdate.length!= 8) {return null;} var dtdate = null; var nyear = par
Seint (strdate.substring (0, 4), 10); var nmonth = ParseiNT (Strdate.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 (Nyea
R, NMonth-1, Nday); if (nyear!= dtdate.getfullyear () | | (nMonth-1)!= dtdate.getmonth () | |
Nday!= dtdate.getdate ()) {return null;} return dtdate; Format of YYYYMMDD

Above this JS to determine whether the date format is a simple example is a small series to share all the content, hope to give you a reference, but also hope that we support the cloud-dwelling community.

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.