Home > front end >  Use JavaScript to get the number of days in a year
Use JavaScript to get the number of days in a year

Time:12-06

Use JavaScript to get the number of days a year
Author: Chen Xixiong
Time to write: 2019/1/16

Here I want to share a get a certain number of days a year, written in a simple JavaScript language,
Note: if you do not specify the year is in accordance with the default current years, the number of days for current year, if the specified year according to the number of days specified year to obtain a specified year
The source code as shown in the figure below:

MyDate ()//the number of days for current year
MyDate (t)//access to specify the year
The function myDate (t) {//t on behalf of the specified parameter
If (t==null)
{
Var Year=new Date (). GetFullYear (), s=0, d;//for current year
For (var I=1; i <13; I++) {
D=new Date (Year, I, 0);//get the number of days a month
S +=d.g etDate ();
};
Return s;
} else if (t & gt; {
=1970)Var Year=new Date (). GetFullYear (), s=0, d;
For (var I=1; i <13; I++) {
D=new Date (t, I, 0).
S +=d.g etDate ();
};
Return s;
} else {
Year of return "wrong";
}
}

CodePudding user response:

Var year=t | | new Date (). GetFullYear ();

Can combine two cycle code

CodePudding user response:

Leap year equation of less complex, directly with,

The function myDate (t) {//t on behalf of the specified parameter
Var Year=t | | new Date (). GetFullYear ();
Return Year % 4==0 & amp; & Year % 100!=0 | | Year %==0 400? 366-365;
}

CodePudding user response:

Or determine the number of days in February only can
The return of new Date (Year, 2, 0). The getDate ()==29? 366-365;

CodePudding user response:

The toddler..

CodePudding user response:

Leap year equation of less complex, directly with,

The function myDate (t) {//t on behalf of the specified parameter
Var Year=t | | new Date (). GetFullYear ();
Return Year % 4==0 & amp; & Year % 100!=0 | | Year %==0 400? 366-365;
}, positive solution on the second floor

CodePudding user response:

Thank you for your advice, in fact I don't know much about the calculation formula of a leap year

CodePudding user response:

Thank you for your advice, in fact I don't know much about the calculation formula of a leap year

CodePudding user response:

Learning the thank you for your sharing

CodePudding user response:

366, 365 is not the problem of a leap month?

CodePudding user response:

Assumes that the revolution of the earth changes, also can use a leap year formula?
  • Related