DZone Snippets is a public source code repository. Easily build up your personal collection of code snippets, categorize them with tags / keywords, and share them with the world

Snippets has posted 5883 posts at DZone. View Full User Profile

Fix Safari's Date#setMonth

10.23.2006
| 3758 views |
  • submit to reddit
        fix safari's Date#setMonth
(function(){
	var set_month = Date.prototype.setMonth;
	Date.prototype.setMonth = function(num){
		if(num <= -1){
			var n = Math.ceil(-num);
			var back_year = Math.ceil(n/12);
			var month = (n % 12) ? 12 - n % 12 : 0 ;
			this.setFullYear(this.getFullYear() - back_year);
			return set_month.call(this, month);
		} else {
			return set_month.apply(this, arguments);
		}
	}
})();