Created
November 21, 2016 20:53
-
-
Save chrisstraw/066cef34f53f6d0db79e34fcac41b2fd to your computer and use it in GitHub Desktop.
This file contains hidden or 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
var regexIso8601 = /^([\+-]?\d{4}(?!\d{2}\b))((-?)((0[1-9]|1[0-2])(\3([12]\d|0[1-9]|3[01]))?|W([0-4]\d|5[0-2])(-?[1-7])?|(00[1-9]|0[1-9]\d|[12]\d{2}|3([0-5]\d|6[1-6])))([T\s]((([01]\d|2[0-3])((:?)[0-5]\d)?|24\:?00)([\.,]\d+(?!:))?)?(\17[0-5]\d([\.,]\d+)?)?([zZ]|([\+-])([01]\d|2[0-3]):?([0-5]\d)?)?)?)?$/; | |
function convertDateStringsToDates(input) { | |
// Ignore things that aren't objects. | |
if (typeof input !== "object") return input; | |
//console.log("transforming", input); | |
for (var key in input) { | |
if (!input.hasOwnProperty(key)) continue; | |
var value = input[key]; | |
var match; | |
// Check for string properties which look like dates. | |
if (typeof value === "string" && (match = value.match(regexIso8601))) { | |
if (match[0].contains("T00:00:00")) { | |
var dtString = match[0].replace("T00:00:00", ""); | |
var dt = new Date(dtString); | |
input[key] = new Date(new Date(dt.getTime() + dt.getTimezoneOffset()*60000)); | |
} else { | |
var milliseconds = Date.parse(match[0]) | |
if (!isNaN(milliseconds)) { | |
var dt = new Date(milliseconds); | |
input[key] = new Date(new Date(dt.getTime() + dt.getTimezoneOffset()*60000)); | |
//input[key] = new Date(milliseconds); | |
} | |
} | |
} else if (typeof value === "object") { | |
// Recurse into object | |
convertDateStringsToDates(value); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment