Solved

how many days in a month using javascript

Posted on 2014-02-03
3
194 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
3 Comments
 
LVL 19

Accepted Solution

by:
Ken Butters earned 500 total points
Comment Utility
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
Comment Utility
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
Comment Utility
works perfect!
0

Featured Post

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

When you need to keep track of a simple list of numbers or strings, the Array object is your most direct tool.  As we saw in my earlier EE Article (http://www.experts-exchange.com/A_3488.html), typical array handling might look like this: (CODE) B…
Having worked on larger scale sites, we found out that you are bound to look at more scalable solutions to integrating widgets, code snippets or complete applications and mesh them into functional sites, in any given composition. To share some of…
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…

744 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

Need Help in Real-Time?

Connect with top rated Experts

17 Experts available now in Live!

Get 1:1 Help Now