1. Before the date that JS obtains n months of a given month, Xiao Ying first tells you the difference between getFullYear () and GetYear ().
①getyear ()
New Date () Console.log (D.getyear ());
Print Result: 116
Xiao Ying Baidu, JS in the Date Object GetYear () method and getFullYear () method differences, such as: JS in the Date Object GetYear () method and getFullYear () method differences found that many people write:
In IE, the year can be obtained correctly: 2016, but under the FF and other browsers: 116.
The reason is that in a browser such as FF, GetYear returns the value of "Current year-1900" (that is, the year Base is 1900)
IE is when today's year is greater than or equal to 2000, the direct 1900 plus, the return of 2016.
But when the above code in the IE run, and found that the result is: 116, Xiao Ying's IE version is:
So Xiao Ying may feel that with the IE version of the update, IE has not supported the GetYear () method. But this is just a small speculation, hehe, we can also try to see what is going on.
②getfullyear ()
New Date (); Console.log (D.getfullyear ());
Print Result: 2016
Use getFullYear () to get the year correctly in IE and FF: 2016
2. Let's take a look at getFullYear (), GetMonth (), GetDate ().
①getfullyear ()
Definition and usage
The getFullYear () method returns a 4-digit number that represents the year.
Browser support
All major browsers support the getFullYear () method
Grammar
Date. getFullYear ()
return value
Type |
Describe |
Number |
A 4-digit number that represents the year. |
Example
New Date (); Console.log (D.getfullyear ()); var d2=new Date (2015,11,05); Console.log (D2.getfullyear ());
Print results
2016
2015
②getmonth ()
Definition and usage
The GetMonth () method returns a number that represents the month. The return value is an integer between 0 (January) and 11 (December).
Note: January is 0, February is 1, and so on.
Browser support
All major browsers support the GetMonth () method
Grammar
Date. GetMonth ()
return value
Type |
Describe |
Number |
The return value is an integer between 0 (January) and 11 (December). |
Instance
New Date (); Console.log (D.getmonth () +1); // because the getmonth () return value is an integer between 0 (January) and 11 (December). So to add 1 var d2=new Date (2015,11,05); Console.log (D2.getmonth ());
Print results
12
11
③getdate ()
Definition and usage
The GetDate () method returns a day of the month.
Browser support
All major browsers support the GetDate () method
Grammar
Date. GetDate ()
return value
Type |
Describe |
Number |
Dateobject refers to the day of the month, using local time. The return value is an integer between 1 and 31.
|
Instance
var New Date (); var dd1 = d.getdate (); var d2=New Date (n, a); var dd2 = d2.getdate (); Console.log (DD1); Console.log (DD2);
Print Results
27
15
3.js gets the date after n months of a given month
① Gets the date 3 months after the current date
varD =NewDate (); D.setmonth (D.getmonth ()+ 3); varYy1 =d.getfullyear (); varMM1 = D.getmonth () +1;//because the getmonth () return value is an integer between 0 (January) and 11 (December). So I'm going to add 1 to it . varDD1 =d.getdate (); if(MM1 < 10) {mm1= ' 0 ' +mm1; } if(DD1 < 10) {dd1= ' 0 ' +dd1; } console.log (Yy1+ '-' + mm1 + '-' + dd1);
Current time: December 27, 2016 printed results 2017-03-27
Current time: September 08, 2016 printed results 2016-12-08
② Gets the date 3 months after the given month
varD =NewDate (2015, 12, 15); //because GetMonth () gets the value of the month only between 0~11 so we'll give it a minus one before we make setmonth ()D.setmonth ((D.getmonth ()-1) + 3); varYy1 =d.getfullyear (); varMM1 = D.getmonth () +1; Console.log (MM1); varDD1 =d.getdate (); if(MM1 < 10) {mm1= ' 0 ' +mm1; } if(DD1 < 10) {dd1= ' 0 ' +dd1; } console.log (Yy1+ '/' + mm1 + '/' + DD1);
Print results
3
2016/03/15
JS gets the date after n months of a given month