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
Solved

how many days in a month using javascript

Posted on 2014-02-03
3
198 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
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

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

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…
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…

839 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