JavaScript Small Features

Source: Internet
Author: User

1. js to determine whether it is a valid date
1234 functioncheck(date){    return (newDate(date).getDate()==date.substring(date.length-2));}//参数date可以格式化为xx-xx-xx或xxxx-xx-xx或用/分割

New Date: If date is an invalid variable, the invalid data is returned

GetDate (): Returns the day of the date, such as 2016-02-11, returns 11, if the number of days in the current year and month does not exist, such as: 2016-02-30, then return is 1, if it is 2016-02-31, then return 2, if the number of days is greater than 31 or non-numeric, The Nan is returned;

Date.substring (date-length-2): The number of days in the acquisition date, as determined by the GETDATE () function, if the date is valid, the number of days is equal, otherwise unequal

Test:

1234567 functioncheck(date){    vardate2 = newDate(date);    console.log(date2);    console.log(date2.getDate());    console.log(date.substring(date.length-2));    return(newDate(date).getDate()==date.substring(date.length-2));}

  

Report:

In general, the validity of the JavaScript validation date can be judged by regular

However, regular expressions cannot accurately verify the validity of dates, and you cannot tell by regular expressions that 1900-02-29 is an illegal date and 2000-02-29 is a legal date, and that regular expressions match up more cumbersome.

The easiest way to do this is to determine whether the date is legal by month (1~28/29/30/31), and you can use an array to represent the number of days in a month, such as daysinmonth=[31,28,31,30,31,30,31,31,30,31,30,31 ], but you will find daysinmonth[1] This value may be 28 or 29, you have to write a function to judge the leap year to solve the problem ...

It takes at least 20 lines of code to do this, and with the date object in JavaScript, we only need 1 lines of code to do this.

function Check (date) {

Return (new date) getDate () ==date.substring (date.length-2));

}

Date is a class date string to be checked (e.g. 2013-01-01, 2013/01/01, 2013/01/32, 2013/02/29), regardless of whether the string is correct

By creating a new Date object (new date), you can identify whether the date is correct and return invalid date if it is incorrect

However, there is a bug that when the value of the date is between 1-31, new date always returns an object regardless of whether the month exists (for example, 2013-02-30 will return the date object Sat Mar 08:00:00 gmt+0800 (China Standard Time)) , the returned result is the date of next month without error

So to get the date with the GETDATE () method (The GETDATE () method of the new date (' 2013-02-30 ') object returns 2)

Date.substring (date.length-2) Gets the part of the last two bits of the string that is the date, which is compared to the date part of the Date object, if the equality is valid, otherwise the date is invalid

If the date is invalid, the left value of ' = = ' is Nan, and the right side no matter what (', null, undefined, Nan, etc.) the result is false

2. JS implements a list item to move in and out of animation effect

1. Use onmouseover events, onmouseout events, Timers SetInterval, and Clearinterval

2. When the onmouseover event is triggered, a timer is opened and the left or right or top or bottom properties are incremented or decreased at a certain time, depending on the entire width of the list item. Stops moving when its left or right or top or bottom property equals 0 or its entire width, indicating that the entire list item has been moved out, and the removal effect is completed

3. Regenerate into a onmouseout event, triggering the onmouseout event to the opposite of the action that triggered the onmouseover event against the left or right or top or bottom properties.

JavaScript Small Features

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.