Home > Web Front-end > JS Tutorial > Get the first day of the next month and the day of the week in javascript_time and date

Get the first day of the next month and the day of the week in javascript_time and date

WBOY
Release: 2016-05-16 17:52:49
Original
2953 people have browsed it

Copy code The code is as follows:

var odatef = new Date();
odatef .setFullYear(2012);
odatef.setMonth(5)
odatef.setDate(1);
fday = ordatef.getDay();

If today is May On the 30th, through the above code, I should get the next month, that is, what day of the week is June 1st? And assign it to the variable fday;
Sure enough, I can get it correctly;
The process is:
1. Execute this odatef.setFullYear(2012); Then the odatef object will be: May 30, 2012 Day;
2. Execute to this sentence odatef.setMonth(5); Then the odatef object will be: June 30, 2012;
3. Execute to this sentence odatef.setDate(1); Then odatef The object will be: June 1, 2012;
4. Execute this sentence ordatef.getDay(); Then what we get is: June 1, 2012, what day of the week it is, exactly what we want !


If today is May 31st, using the same code above, I cannot correctly get the day of the week next month.
Reason:
1. Execute this odatef.setFullYear(2012); Then the odatef object will be: May 31, 2012;
2. Execute this odatef.setMonth(5); Then the odatef object will be: June 31, 2012; the problem lies in the second step: June does not have a 31st, so it will jump to July. Then the odatef object will be: July 2012 On the 31st, if there is no 31st in July, continue to jump to August until there is a month with the 31st;
3. Execute this sentence odatef.setDate(1); Then the odatef object will be: July 2012 Month 1;
4. Execute this sentence ordatef.getDay(); Then what we get is: July 1, 2012, what day of the week it is, which is not what we want! ~
Solution: Change the position of the statement and set the date first, then the month!
Copy code The code is as follows:

var odatef = new Date();
odatef .setFullYear(2012);
odatef.setDate(1);
odatef.setMonth(6)
fday = ordatef.getDay();

1. Execute this sentence odatef.setFullYear(2012); Then the odatef object will be: May 31, 2012; 2. Execute this sentence odatef.setDate(1); Then the odatef object will be: May 1, 2012;
3. Execute this sentence odatef.setMonth(5); Then the odatef object will be: June 1, 2012;
4. Execute this sentence ordatef.getDay(); Then what you get is: 2012 June 1st, what day of the week is it! ~~~
Summary: It is necessary to understand each statement, what is the result of execution, or what is returned. I always thought that it was just to set the year and month. No consideration is given to the results or what is returned after setting!
Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template