code_junkie

Calculate last day of month in javascript

Nov 11th, 2011
78
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.91 KB | None | 0 0
  1. d = new Date(); d.setFullYear(2008, 11, 0); // Sun Nov 30 2008
  2.  
  3. var month = 0; // January
  4. var d = new Date(2008, month + 1, 0);
  5. alert(d); // last day in January
  6.  
  7. IE 6: Thu Jan 31 00:00:00 CST 2008
  8. IE 7: Thu Jan 31 00:00:00 CST 2008
  9. IE 8: Beta 2: Thu Jan 31 00:00:00 CST 2008
  10. Opera 8.54: Thu, 31 Jan 2008 00:00:00 GMT-0600
  11. Opera 9.27: Thu, 31 Jan 2008 00:00:00 GMT-0600
  12. Opera 9.60: Thu Jan 31 2008 00:00:00 GMT-0600
  13. Firefox 2.0.0.17: Thu Jan 31 2008 00:00:00 GMT-0600 (Canada Central Standard Time)
  14. Firefox 3.0.3: Thu Jan 31 2008 00:00:00 GMT-0600 (Canada Central Standard Time)
  15. Google Chrome 0.2.149.30: Thu Jan 31 2008 00:00:00 GMT-0600 (Canada Central Standard Time)
  16. Safari for Windows 3.1.2: Thu Jan 31 2008 00:00:00 GMT-0600 (Canada Central Standard Time)
  17.  
  18. int_d = new Date(2008, 11+1,1);
  19. d = new Date(int_d - 1);
  20.  
  21. function daysInMonth(iMonth, iYear)
  22. {
  23. return 32 - new Date(iYear, iMonth, 32).getDate();
  24. }
Add Comment
Please, Sign In to add comment