If you provide 0
as the dayValue
in Date.setFullYear
you get the last day of the previous month:
d = new Date(); d.setFullYear(2008, 11, 0); // Sun Nov 30 2008
There is reference to this behaviour at mozilla. Is this a reliable cross-browser feature or should I look at alternative methods?
set month you need to date and then set the day to zero ,so month begin in 1 - 31 in date function then get the last day^^
I find this to be the best solution for me. Let the Date object calculate it for you.
Setting day parameter to 0 means one day less than first day of the month which is last day of the previous month.
Output differences are due to differences in the
toString()
implementation, not because the dates are different.Of course, just because the browsers identified above use 0 as the last day of the previous month does not mean they will continue to do so, or that browsers not listed will do so, but it lends credibility to the belief that it should work the same way in every browser.
I would use an intermediate date with the first day of the next month, and return the date from the previous day:
This works for me. Will provide last day of given year and month:
Below function gives the last day of the month :
Similarly we can get first day of the month :