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 Tags

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Replace String Characters in JavaScript Replace String Characters in JavaScript Mar 11, 2025 am 12:07 AM

Replace String Characters in JavaScript

Custom Google Search API Setup Tutorial Custom Google Search API Setup Tutorial Mar 04, 2025 am 01:06 AM

Custom Google Search API Setup Tutorial

Example Colors JSON File Example Colors JSON File Mar 03, 2025 am 12:35 AM

Example Colors JSON File

8 Stunning jQuery Page Layout Plugins 8 Stunning jQuery Page Layout Plugins Mar 06, 2025 am 12:48 AM

8 Stunning jQuery Page Layout Plugins

Build Your Own AJAX Web Applications Build Your Own AJAX Web Applications Mar 09, 2025 am 12:11 AM

Build Your Own AJAX Web Applications

What is 'this' in JavaScript? What is 'this' in JavaScript? Mar 04, 2025 am 01:15 AM

What is 'this' in JavaScript?

Improve Your jQuery Knowledge with the Source Viewer Improve Your jQuery Knowledge with the Source Viewer Mar 05, 2025 am 12:54 AM

Improve Your jQuery Knowledge with the Source Viewer

10 Mobile Cheat Sheets for Mobile Development 10 Mobile Cheat Sheets for Mobile Development Mar 05, 2025 am 12:43 AM

10 Mobile Cheat Sheets for Mobile Development

See all articles