Created
March 23, 2010 17:57
-
-
Save kara-ryli/341466 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(function () { | |
"use strict"; | |
var TO_ISO8601_DEFAULTS = { | |
year: true, | |
date: true, | |
time: true, | |
seconds: true, | |
milliseconds: true, | |
utc: false, | |
zone: true | |
}; | |
function zeroPad(oInt) { | |
return (oInt < 10 ? "0" : "") + oInt; | |
} | |
Date.prototype.setISO8601 = (function () { | |
var REG_EXP = /(\d\d\d\d)(?:\-?(\d\d)(?:\-?(\d\d)(?:[T ](\d\d)(?::?(\d\d)(?::?(\d\d)(?:\.(\d+))?)?)?(?:Z|(?:([\-+])(\d\d)(?::?(\d\d))?)?)?)?)?)?/; | |
return function (string) { | |
var d, offset, date, time; | |
d = string.match(REG_EXP); | |
offset = 0; | |
date = new Date(d[1], 0, 1); | |
if (d[2]) { | |
date.setMonth(d[2] - 1); | |
} | |
if (d[3]) { | |
date.setDate(d[3]); | |
} | |
if (d[4]) { | |
date.setHours(d[4]); | |
} | |
if (d[5]) { | |
date.setMinutes(d[5]); | |
} | |
if (d[6]) { | |
date.setSeconds(d[6]); | |
} | |
if (d[7]) { | |
date.setMilliseconds(Number("0." + d[7]) * 1000); | |
} | |
if (d[8]) { | |
offset = (Number(d[9]) * 60) + Number(d[10]); | |
offset *= ((d[8] === '-') ? 1 : -1); | |
} | |
offset -= date.getTimezoneOffset(); | |
time = Number(date) + (offset * 60 * 1000); | |
this.setTime(time); | |
}; | |
}()); | |
Date.prototype.toISO8601 = function (oConfig) { | |
var result = "", i, z, config = {}; | |
if (! oConfig) { | |
config = TO_ISO8601_DEFAULTS; | |
} | |
else { | |
for (i in TO_ISO8601_DEFAULTS) { | |
if (TO_ISO8601_DEFAULTS.hasOwnProperty(i)) { | |
config[i] = (typeof oConfig[i] !== "undefined") ? oConfig[i] : TO_ISO8601_DEFAULTS[i]; | |
} | |
} | |
} | |
if (config.year) { | |
result += (this.utc ? this.getUTCFullYear() : this.getFullYear()); | |
} | |
if (config.year && config.date) { | |
result += '-'; | |
} | |
if (config.date) { | |
result += zeroPad((this.utc ? this.getUTCMonth() : this.getMonth()) + 1) + '-' + zeroPad(this.utc ? this.getUTCDate() : this.getDate()); | |
} | |
if (config.date && config.time) { | |
result += "T"; | |
} | |
if (config.time) { | |
result += zeroPad((this.utc ? this.getUTCHours() : this.getHours())) + ':' + zeroPad(this.utc ? this.getUTCMinutes() : this.getMinutes()); | |
if (config.seconds) { | |
result += ':' + zeroPad(this.utc ? this.getUTCSeconds() : this.getSeconds()); | |
if (config.milliseconds) { | |
result += '.' + zeroPad(this.utc ? this.getUTCMilliseconds() : this.getMilliseconds()); | |
} | |
} | |
if (config.zone && config.utc) { | |
result += '+0:00'; | |
} | |
else if (config.zone) { | |
z = this.getTimezoneOffset() / 60; | |
result += ((z > 0) ? '+' : '') + Math.floor(z) + ':' + zeroPad((z - Math.floor(z)) * 100 / 60); | |
} | |
} | |
return result; | |
}; | |
Date.ISO8601 = function (string) { | |
var d = new Date(); | |
d.setISO8601(string); | |
return d; | |
}; | |
}()); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment