About bugs in setmonth

Source: Internet
Author: User

I have no time to update my blog posts next weekend. I will write an article this evening. Yesterday was the 31st, and this date was a bit special. The signing of the company's platform also encountered a problem. This problem actually appeared six months ago. I checked the source code and didn't see why, then let's leave it alone. Recently, the credit Mall of the company's platform is ready to go online, and the points for sign-In are also useful, so this bug cannot be solved.

After talking about this, you may not know what the problem is. Thank you for your patience. Everyone should know about setmonth. Set month. Here is an example:

VaR taday = new date ();

Taday. setmonth (taday. getmonth () + 1 );

Alert (taday. getmonth ());

Now it's July 22, November 1, so we should all know that it was 11 (the month started from 0), but if you set the computer time to July 15, October 31, will the result be 10? I tried to know that he would not print 10. Instead, I printed 11. Why? The reason is that JS computing calculates a month as 30 days, and an extra day automatically overflows. It is calculated as next month, that is, November, if you add 1, it will become December, and the print will become 11. How can we solve this problem? Obviously, since no JS contains no 31st, we should set the date to 1st so that the time will not overflow.

VaR taday = new date ();

Taday. setdata (1 );

Taday. setmonth (taday. getmonth () + 1 );

Alert (taday. getmonth ());

This solves the problem of setmonth on the 31st. This problem was finally understood after a whole day yesterday. In fact, the time is basically based on logic. It seems that there is no problem at all. At last, I thought it would be a problem with setmonth, after printing it out, I found it was a problem. I wasted so much time and thought it was not worth it. If I knew this problem, I would not have spent so much time.

About bugs in setmonth

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.