A simple way to determine whether the input string is a date format _javascript tips

Source: Internet
Author: User

The example below is as follows:

function IsDate (datestring) {
  if (Datestring.trim () = "") return true;
  var r=datestring.match (/^ (\d{1,4}) (-|\/) (\d{1,2}) \2 (\d{1,2}) $/); 
  if (r==null) {
   alert ("Please enter a properly formatted date \n\r date format: yyyy-mm-dd\n\r example  : 2008-08-08\n\r");
  return false;
  }
  var d=new Date (R[1],r[3]-1,r[4]);  
  var num = (d.getfullyear () ==r[1]&& (D.getmonth () +1) ==r[3]&&d.getdate () ==r[4]);
  if (num==0) {
   alert ("Please enter a properly formatted date \n\r date format: yyyy-mm-dd\n\r example  : 2008-08-08\n\r");
  }
  return (num!=0);
 } 

The above decision to enter the string is a date format is a simple way to share all the content of the small, 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.