JavaScript to get the next month a number, is the day of the week _ time date

Source: Internet
Author: User

Copy Code code as follows:

var Odatef = new Date ();
Odatef.setfullyear (2012);
Odatef.setmonth (5)
Odatef.setdate (1);
Fday = Ordatef.getday ();

If today is May 30, through the above code, I should be getting to the next month, that is, June 1 is the day of the week? and assigning value to variable Fday;
Sure enough, I could get it right;
The process is:
1. Implementation of the sentence Odatef.setfullyear (2012); So Odatef to the like: May 30, 2012;
2. Implementation to this sentence odatef.setmonth (5); So Odatef to the like: June 30, 2012;
3. Implementation to this sentence odatef.setdate (1); So Odatef to the like: June 1, 2012;
4. Implementation to this sentence ordatef.getday (); Then get the: June 1, 2012, is the day of the week, exactly what we want!


If today is May 31, the same is the code above, I can not get the right to the next month is the day of the week.
Reason:
1. Implementation of the sentence Odatef.setfullyear (2012); So Odatef to the like: May 31, 2012;
2. Implementation to this sentence odatef.setmonth (5); So Odatef to the like: June 31, 2012; The problem is in this second step: June, there is no number 31st, so it will jump to July so Odatef object will be: July 31, 2012, if July also not 31st, continue to jump to August until the month of 31st;
3. Implementation to this sentence odatef.setdate (1); So Odatef to the like: July 1, 2012;
4. Implementation to this sentence ordatef.getday (); Then get the: July 1, 2012, is the day of the week, not what we want! ~
Solution: Change the position of the statement first set the date, and then set the month!
Copy Code code as follows:

var Odatef = new Date ();
Odatef.setfullyear (2012);
Odatef.setdate (1);
Odatef.setmonth (6)
Fday = Ordatef.getday ();

1. Implementation of the sentence Odatef.setfullyear (2012);  So Odatef to the like: May 31, 2012; 2. Execute to this sentence odatef.setdate (1); So Odatef to the like: May 1, 2012;
3. Implementation to this sentence odatef.setmonth (5); So Odatef to the like: June 1, 2012;
4. Implementation to this sentence ordatef.getday (); So the acquisition is: June 1, 2012, is the day of the week! ~~~
Summary: To find out what each statement is, what the result of the execution is, or what is returned. Previously thought, is to set the year, month. Do not consider the result after setting or return something!

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.