JS How to get the number of days in a specified month

Source: Internet
Author: User

JS How to get the number of days for a specified month:
Sometimes you need to get the number of days in a given year of January, because the number of days per month is different, and the number of days is different for one months, for example, in February, when the number of days in a leap year is a difference, here's a simple way to get the number of days in a specified month in a year.
The code is as follows:

functionGetdadys (whichyear,whichmonth) {varNextmoth=whichmonth+1varNextyear=whichyear; if(nextmoth==13) {Nextmoth=1; Nextyear++; }  varthecurrentdate=whichyear+ "-" +whichmonth+ "-1"; varthenextdate=nextyear+ "-" +nextmoth+ "-1"; varYearobjone=NewDate (thecurrentdate); varyearobjtwo=NewDate (thenextdate); varMilliseconds=yearobjtwo.gettime ()-yearobjone.gettime ()vardaymilliseconds=3600*24*1000; return(Milliseconds/daymilliseconds);}document.write (Getdadys (2012,2));

the code above can calculate the number of days in a given year in January. The output result is 29. Here's the implementation process:
I. Principle of implementation:
The simple principle is to get the time stamp gap at the beginning of the next month and the beginning of the month to get the number of days, so that you get the number of milliseconds that the days of the month have, and then divide that number of milliseconds by the number of milliseconds you have per day, so that you get the number of days in the month.
two. Code comments:
1.function Getdadys (whichyear,whichmonth) {}, declares a function to get the number of days of the month, has two parameters, the first parameter is the year to get the month, and the second parameter gets the number of days of the month.
2.var nextmoth=whichmonth+1, next month.
3.if (nextmoth==13) determines whether the value of Nextmoth equals 12.
4.nextmoth=1, more than 12 so that's starting in January of the next year.
5.whichyear++, of course, the year also to add 1.
6.var thecurrentdate=whichyear+ "-" +whichmonth+ "-1", the connection string, makes it into this form in 2012-2-1.
7.var thenextdate=nextyear+ "-" +nextmoth+ "-1", and the same principle as above.
8.var yearobjone=new Date (thecurrentdate), which creates the time object.
9.var milliseconds=yearobjtwo.gettime ()-yearobjone.gettime () to find the millisecond difference between two months.
10.var daymilliseconds=3600*24*1000, gets the number of milliseconds in a day.
11.return (Milliseconds/daymilliseconds), returns the number of days, the total number of milliseconds divided by the number of milliseconds in a day is the number of days.
three. Related reading:
1.Date You can refer to the description of the date () constructor parameter in JavaScript for a chapter.
The 2.getTime () function can refer to thegetTime () method section of the Date object of JAvascript.

The original address is: http://www.softwhy.com/forum.php?mod=viewthread&tid=8924

For more information, refer to: http://www.softwhy.com/javascript/

JS How to get the number of days in a specified month

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.