Skip to content

Instantly share code, notes, and snippets.

@hustcc
Created June 6, 2016 10:48
Show Gist options
  • Save hustcc/dfff33870aeea9e3b8b9f5965a441044 to your computer and use it in GitHub Desktop.
Save hustcc/dfff33870aeea9e3b8b9f5965a441044 to your computer and use it in GitHub Desktop.
parse = function(input) {
if (input instanceof Date) {
return input;
} else if (!isNaN(input)) {
return new Date(input);
} else if (/^\d+$/.test(input)) {
return new Date(parseInt(input, 10));
} else {
var s = (input || '').trim();
s = s.replace(/\.\d+/, ''); // remove milliseconds
s = s.replace(/-/, '/').replace(/-/, '/');
s = s.replace(/T/, ' ').replace(/Z/, ' UTC');
s = s.replace(/([\+\-]\d\d)\:?(\d\d)/, ' $1$2'); // -04:00 -> -0400
return new Date(s);
}
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment