Home > Web Front-end > JS Tutorial > JavaScript code snippet to determine whether a date is valid

JavaScript code snippet to determine whether a date is valid

php中世界最好的语言
Release: 2018-03-16 16:09:39
Original
1887 people have browsed it

This time I will bring you a code snippet to determine whether a date is valid. JavaScript. What are the precautions for using JavaScript to determine whether a date is valid? . Here is a practical case. Let’s take a look. one time.

JavaScript is becoming more and more popular, it has become the first choice for front-end development, and using NodeJS based on JavaScript language, we can also develop high-performance Back-end services, and I even see JavaScript appearing in the hardwareprogramming field. JavaScript is gradually evolving into a versatile development language.

But using JavaScript well is not easy. In addition to mastering its syntax and knowing how to write high-quality code, you also need to know how to solve problems that will be encountered in almost every project. There are many third-party libraries that can solve these problems, such as determining dates, highlighting text, limiting the number of characters, etc., but these libraries may not be created just to solve this problem, which means you need Introducing a lot of irrelevant code will make your entire system bloated and will also affect system performance. My approach is to collect and use common JavaScript snippets and use them first when possible. Below are 10 pieces of practical JavaScript code that I have collected. Based on them, you can also create more powerful JS plug-ins or functions.

The date function that comes with JavaScript is still too simple, and it is difficult to meet the needs of parsing and judging different date formats in real projects. JQuery There are also some third-party libraries to make date-related processing simple, but sometimes you may only need a very simple function and do not want to introduce a huge third-party library. At this time, you can use the following date verification code, which allows you to customize the date format and verify the date validity.

function isValidDate(value, userFormat) { 
  // Set default format if format is not provided
  userFormat = userFormat || 'mm/dd/yyyy'; 
  // Find custom delimiter by excluding
  // month, day and year characters
  var delimiter = /[^mdy]/.exec(userFormat)[0]; 
  // Create an array with month, day and year
  // so we know the format order by index
  var theFormat = userFormat.split(delimiter); 
  // Create array from user date
  var theDate = value.split(delimiter); 
  function isDate(date, format) {    var m, d, y, i = 0, len = format.length, f;    for (i; i < len; i++) {
      f = format[i];      if (/m/.test(f)) m = date[i];      if (/d/.test(f)) d = date[i];      if (/y/.test(f)) y = date[i];
    }    
return
 (
      m > 0 && m < 13 &&
      y && y.length === 4 &&
      d > 0 &&      // Check if it&#39;s a valid day of the month
      d <= (new Date(y, m, 0)).
getDate
()
    );
  } 
  return isDate(theDate, theFormat);
}
Copy after login

Usage:
The following call returns false, because there are no 31 days in November

isValidDate(&#39;dd-mm-yyyy&#39;, &#39;31/11/2012&#39;)
Copy after login

I believe you have mastered the method after reading the case in this article. For more exciting information, please pay attention to php Chinese NetOther related articles!

Recommended reading:

Arrary method of JS

##Event Loop of JavaScript running mechanism

JavaScript running mechanism Why JavaScript is single-threaded

The above is the detailed content of JavaScript code snippet to determine whether a date is valid. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template