Js string date directly compare the size

Source: Internet
Author: User
Js string date directly compare the size today met a bug, the project running for one year suddenly has a wrong date, and finally found that it is the reason for the direct comparison of string date.


The debugging result is as follows:


Many people say that js string dates can be directly compared and compared, which is more intelligent.

Alert ("2016-10-01"> "2016-10-02"); // false
// No matter whether js is compared by time format or string format, false is returned.

Therefore, we can compare


However, when the string date is not regular, for example:

Alert ("2016-10-4"> "2016-10-30"); // true

// Here js compares it as a string, so it cannot be directly compared and needs to be converted to the date format

Var date1 = new Date ("2016-10-4 ");
Var date2 = new Date ("2016-10-10 ");

Alert (date1> date2); // false


The same is true for string dates in yy/mm/dd format.

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.