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
May 16, 2016 pm 05:52 PM
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!
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

Hot Article
Repo: How To Revive Teammates
3 weeks ago
By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
1 weeks ago
By 尊渡假赌尊渡假赌尊渡假赌
How Long Does It Take To Beat Split Fiction?
3 weeks ago
By DDD
Hello Kitty Island Adventure: How To Get Giant Seeds
3 weeks ago
By 尊渡假赌尊渡假赌尊渡假赌

Hot tools Tags

Hot Article
Repo: How To Revive Teammates
3 weeks ago
By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
1 weeks ago
By 尊渡假赌尊渡假赌尊渡假赌
How Long Does It Take To Beat Split Fiction?
3 weeks ago
By DDD
Hello Kitty Island Adventure: How To Get Giant Seeds
3 weeks ago
By 尊渡假赌尊渡假赌尊渡假赌

Hot Article Tags

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics

Replace String Characters in JavaScript

Custom Google Search API Setup Tutorial

8 Stunning jQuery Page Layout Plugins

Improve Your jQuery Knowledge with the Source Viewer

10 Mobile Cheat Sheets for Mobile Development
