?
Solved

how many days in a month using javascript

Posted on 2014-02-03
3
Medium Priority
?
205 Views
Last Modified: 2014-02-03
Hi,

I need a code to know how many days are in a specific month of a specific year, any idea? I need it in javascript
0
Comment
Question by:joyacv2
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
3 Comments
 
LVL 19

Accepted Solution

by:
Ken Butters earned 2000 total points
ID: 39830126
found the following code that exists on several different sites.


function numberOfDays(year, month) {
    var d = new Date(year, month, 0);
    return d.getDate();
}

Open in new window


The month input paramter on the constructor of date takes a value from 0 to 11, but this solution requires the month AFTER then one you really want...

The day is from 1 to 31

Using a day parameter of 1 forces the date constructor to back up to the last day of the prior month..

d.getDate gives you that last day.

Example request:


var numDays = numberOfDays(2012,6);

That will return number of days in June, 2012.
0
 
LVL 38

Expert Comment

by:Tom Beck
ID: 39830137
Since only February ever changes, you just need to test for leap year.
//year to test example
	var yr = 1990;
	// check for leap year, February number of days (28 or 29)
	var febDays = (((yr % 100 !== 0) && (yr % 4 === 0)) || (yr % 400 === 0)) ? 29 : 28;

Open in new window

0
 
LVL 1

Author Closing Comment

by:joyacv2
ID: 39830147
works perfect!
0

Featured Post

On Demand Webinar: Networking for the Cloud Era

Did you know SD-WANs can improve network connectivity? Check out this webinar to learn how an SD-WAN simplified, one-click tool can help you migrate and manage data in the cloud.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

I've been trying to accomplish this for a while and it just struck me yesterday how to accomplish this task. I have done searches all over the internet looking for ways to email pages from my applications and finally I have done it!!! Every single s…
Originally, this post was published on Monitis Blog, you can check it here . In business circles, we sometimes hear that today is the “age of the customer.” And so it is. Thanks to the enormous advances over the past few years in consumer techno…
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…
Suggested Courses

752 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question