Over a million developers have joined DZone.
{{announcement.body}}
{{announcement.title}}

Fix Safari's Date#setMonth

DZone's Guide to

Fix Safari's Date#setMonth

·
Free Resource
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);
		}
	}
})();
Topics:

Opinions expressed by DZone contributors are their own.

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}