Today is:
    | Home | Web Technology | Language | Articles | | About-Us | Contact-Us |

JS Tutorial

    JS Articles

  • FAQ
    

JavaScript Frequently Ask Question


How to determine number of days with JavaScript?

by Puthyrak Kang
September 17, 2009

JavaScript does not have a built-in function to determine a number of days in a given month of a given year. However, JavaScript Date object allows you to pass an overflow number to the day parameter which expects an integer number from 0 (January) to 11 (December). With overflow day, JavaScript creates a date in the next month. The number of days in the queried month is the number days that the resulting date overlaps into the next month.

<script type="text/javascript">
function daysInMonth(month, year){
   var nbrOfDays = 32 - new Date(month, year, 32).getDate();
   return nbrOfDays;

}

//Calculate the number of day of January, 2010
alert("Number of day in January, 2010 is: "+ daysInMonth(1,2010));

//Calculate the number of day of the current month
var cDay = new Date();
alert("Number of days in the current month is: "
       +daysInMonth(cDay.getMonth(),cDay.getFullYear()));
</script>


    References

    (1) Aland Shalloway & James R. Trott, Design Patterns Explained, Second Edition.

    (2) Allen Holub, Holub on Patterns, Learning Design Patterns by Looking at Code

    (3) Eric Evans, Domain-Driven Design, Tackling complexity in the heart of software.

    Advertisement

puthik.com ©2008