Additionally, how do I retrieve the number of days of a given month?
Aside from calculating it yourself (and consequently having to get leap years right), you can use a Date calculation to do it:
var y= 2010, m= 11; // December 2010 - trap: months are 0-based in JS
var next= Date.UTC(y, m+1); // timestamp of beginning of following month
var end= new Date(next-1); // date for last second of this month
var lastday= end.getUTCDate(); // 31
In general for timestamp/date calculations I'd recommend using the UTC-based methods of Date, like getUTCSeconds
instead of getSeconds()
, and Date.UTC
to get a timestamp from a UTC date, rather than new Date(y, m)
, so you don't have to worry about the possibility of weird time discontinuities where timezone rules change.