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!