Simple JavaScript function is used to check if the date is valid.
function isValidDate(s) { var bits = s.split('/'); var d = new Date(bits[2] + '/' + bits[1] + '/' + bits[0]); return !!(d && (d.getMonth() + 1) == bits[1] && d.getDate() == Number(bits[0])); } //测试 var currentDate = new Date('31/09/2011'); console.log(isValidDate(currentDate.toString())); console.log(isValidDate('30/09/2011'));
JavaScript function is used to get the number of days in a month.
function daysInMonth(month, year) { return new Date(year, month, 0).getDate(); } //测试 daysInMonth('09','2011'); //输出: 30
See also:- jQuery Get future date
- JavaScript date displays year error
- jQuery date and time function - complete list
JavaScript function is used to check if the date is valid and if it is invalid, it will be set to the last day of the month.
/* 检查日期是否有效,如果无效则恢复到当月最后一天 */ validateDateLastDayMonth: function() { /* 辅助函数 */ function isValidDate(s) { var bits = s.split('/'); var d = new Date(bits[2] + '/' + bits[1] + '/' + bits[0]); return !!(d && (d.getMonth() + 1) == bits[1] && d.getDate() == Number(bits[0])); } function daysInMonth(month, year) { return new Date(year, month, 0).getDate(); } /* 初始化 */ var currentDate = new Date(), currentMonth = currentDate.getMonth() + 1, lastDayOfMonth = new Date(currentDate.getFullYear(), (currentDate.getMonth() - 1), 0).getDate(), departureDate = FCL.DATETIME.futureDateDays(14), depDate = departureDate.split('/'), departureDateMonth = depDate[1]; if (departureDateMonth != currentMonth) { departureDate = FCL.DATETIME.leadingZero(lastDayOfMonth) +'/'+ FCL.DATETIME.leadingZero(currentMonth) +'/'+ depDate[2]; } /* 验证日期 */ if (!isValidDate(departureDate.toString())) { var bits = departureDate.split('/'); departureDate = FCL.DATETIME.leadingZero(daysInMonth(bits[1],bits[2])) +'/'+ FCL.DATETIME.leadingZero(currentMonth) +'/'+ depDate[2]; } $('input[name="depDate"]').val(departureDate); }
jQuery date verification FAQs (FAQs)
How to use jQuery to verify dates in a specific format?
jQuery provides a method called $.datepicker.parseDate
that can be used to verify dates in a specific format. This method takes two parameters: date format and date string. If the date string matches the format, the method returns a Date object. If it does not match, an error will be raised. Examples are as follows:
try { $.datepicker.parseDate('dd/mm/yy', '31/12/2020'); console.log('日期有效'); } catch (error) { console.log('日期无效'); }
What is a jQuery verification plugin and how do you use it for date verification?
jQuery Verification Plugin provides a powerful client form verification framework. It includes built-in verification methods for common needs including date verification. To use it, you need to include the plugin in your project and call the validate
method on the form. You can specify rules for each form field, including date fields. For example:
$('form').validate({ rules: { dateField: { date: true } } });
How to use jQuery to verify date range?
To verify the date range, you can use the $.datepicker.parseDate
method to convert the date string to a Date object and then compare. Examples are as follows:
var startDate = $.datepicker.parseDate('dd/mm/yy', '01/01/2020'); var endDate = $.datepicker.parseDate('dd/mm/yy', '31/12/2020'); if (startDate > endDate) { console.log('开始日期晚于结束日期'); } else { console.log('日期范围有效'); }
Can I use jQuery to verify the date without using the plugin?
Yes, you can use the built-in JavaScript Date object to verify dates without using plug-ins. However, this method is not as flexible and more complex as using plug-ins or $.datepicker.parseDate
. Examples are as follows:
var dateString = '31/12/2020'; var dateParts = dateString.split('/'); var dateObject = new Date(+dateParts[2], dateParts[1] - 1, +dateParts[0]); if (dateObject.getDate() == dateParts[0] && dateObject.getMonth() == dateParts[1] - 1 && dateObject.getFullYear() == dateParts[2]) { console.log('日期有效'); } else { console.log('日期无效'); }
The rest of the FAQ content is similar to the above, but the description of the problem and the code example have been slightly adjusted. In order to avoid duplication, I will not repeat it here. The core idea is to verify the validity of dates based on JavaScript's Date
object and jQuery's $.datepicker.parseDate
method. Different problems only differ in verification conditions, such as date format, date range, leap year, etc.
The above is the detailed content of jQuery Check if Date is Valid. For more information, please follow other related articles on the PHP Chinese website!

Python and JavaScript each have their own advantages, and the choice depends on project needs and personal preferences. 1. Python is easy to learn, with concise syntax, suitable for data science and back-end development, but has a slow execution speed. 2. JavaScript is everywhere in front-end development and has strong asynchronous programming capabilities. Node.js makes it suitable for full-stack development, but the syntax may be complex and error-prone.

JavaScriptisnotbuiltonCorC ;it'saninterpretedlanguagethatrunsonenginesoftenwritteninC .1)JavaScriptwasdesignedasalightweight,interpretedlanguageforwebbrowsers.2)EnginesevolvedfromsimpleinterpreterstoJITcompilers,typicallyinC ,improvingperformance.

JavaScript can be used for front-end and back-end development. The front-end enhances the user experience through DOM operations, and the back-end handles server tasks through Node.js. 1. Front-end example: Change the content of the web page text. 2. Backend example: Create a Node.js server.

Choosing Python or JavaScript should be based on career development, learning curve and ecosystem: 1) Career development: Python is suitable for data science and back-end development, while JavaScript is suitable for front-end and full-stack development. 2) Learning curve: Python syntax is concise and suitable for beginners; JavaScript syntax is flexible. 3) Ecosystem: Python has rich scientific computing libraries, and JavaScript has a powerful front-end framework.

The power of the JavaScript framework lies in simplifying development, improving user experience and application performance. When choosing a framework, consider: 1. Project size and complexity, 2. Team experience, 3. Ecosystem and community support.

Introduction I know you may find it strange, what exactly does JavaScript, C and browser have to do? They seem to be unrelated, but in fact, they play a very important role in modern web development. Today we will discuss the close connection between these three. Through this article, you will learn how JavaScript runs in the browser, the role of C in the browser engine, and how they work together to drive rendering and interaction of web pages. We all know the relationship between JavaScript and browser. JavaScript is the core language of front-end development. It runs directly in the browser, making web pages vivid and interesting. Have you ever wondered why JavaScr

Node.js excels at efficient I/O, largely thanks to streams. Streams process data incrementally, avoiding memory overload—ideal for large files, network tasks, and real-time applications. Combining streams with TypeScript's type safety creates a powe

The differences in performance and efficiency between Python and JavaScript are mainly reflected in: 1) As an interpreted language, Python runs slowly but has high development efficiency and is suitable for rapid prototype development; 2) JavaScript is limited to single thread in the browser, but multi-threading and asynchronous I/O can be used to improve performance in Node.js, and both have advantages in actual projects.


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

EditPlus Chinese cracked version
Small size, syntax highlighting, does not support code prompt function

Zend Studio 13.0.1
Powerful PHP integrated development environment

PhpStorm Mac version
The latest (2018.2.1) professional PHP integrated development tool

VSCode Windows 64-bit Download
A free and powerful IDE editor launched by Microsoft

mPDF
mPDF is a PHP library that can generate PDF files from UTF-8 encoded HTML. The original author, Ian Back, wrote mPDF to output PDF files "on the fly" from his website and handle different languages. It is slower than original scripts like HTML2FPDF and produces larger files when using Unicode fonts, but supports CSS styles etc. and has a lot of enhancements. Supports almost all languages, including RTL (Arabic and Hebrew) and CJK (Chinese, Japanese and Korean). Supports nested block-level elements (such as P, DIV),
