adambabik
7/6/2010 - 12:47 PM

toISO8601.js

Number.prototype.putZero = function() {
	if (this >= 0 && this < 10) {
		return '0' + this;
	} else {
		return this;
	}
};

Date.prototype.toISO8601 = function () {		
		var _date = {
			year: this.getFullYear(),
			month: parseInt(this.getMonth()+1),
			day: this.getDate().putZero(),
			hour: this.getHours().putZero(),
			minutes: this.getMinutes().putZero(),
			seconds: this.getSeconds().putZero()
		};
		
		var new_date = _date.year + '-' + _date.month.putZero() + '-' + _date.day + 'T' + _date.hour + ':' + _date.minutes + ':' + _date.seconds + 'Z';
		
		return new_date;
}