Created
August 26, 2010 00:13
-
-
Save natevw/550549 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 toDate(rfc3399) { | |
var parts, date, time, MA; | |
// TODO: ES5 conformant Date.parse can handle RFC3399 directly! | |
// "2010-07-28T00:00:00.5Z" to "28 Jul 2010 00:00:01 GMT" | |
// "2010-07-28T00:00:00.5-07:00" to "28 Jul 2010 00:00:01 GMT" | |
parts = rfc3399.split("T"); | |
date = parts[0]; | |
time = parts[1]; | |
parts = date.split("-").map(function (n) { | |
return parseInt(n, 10); | |
}); | |
MA = ["Month abbreviations", | |
"Jan", "Feb", "Mar", "Apr", | |
"May", "Jun", "Jul", "Aug", | |
"Sep", "Oct", "Nov", "Dec"]; | |
date = [parts[2], MA[parts[1]], parts[0]].join(" "); | |
time = time.replace("Z", " GMT"); | |
time = time.replace(/([+-])(\d\d):(\d\d)$/, " $1$2$3"); | |
parts = 0; | |
time = time.replace(/[0-9]{2}\.[0-9]+ /, function (m) { | |
var intSecs = parseInt(m); | |
parts = parseFloat(m) - intSecs; | |
var intSecs = intSecs.toFixed(); | |
return (intSecs.length == 2) ? intSecs : "0" + intSecs; | |
}); | |
date = new Date(date + " " + time); | |
date.setMilliseconds(parts * 1000); | |
return date; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment