Skip to content

Instantly share code, notes, and snippets.

@jaromero
Created October 16, 2015 22:44
Show Gist options
  • Save jaromero/f4975880abecc522a2a9 to your computer and use it in GitHub Desktop.
Save jaromero/f4975880abecc522a2a9 to your computer and use it in GitHub Desktop.
Kludgey monkey-patched ES6 UTC-enabled Date()
// Do not use if you need something else from Date
// This only naively looks for a date with the format YYYY-MM-DD
// No other cases are covered or guaranteed to still work
// This is just a demo
// Save the old Date thing
var _Date = Date;
// Override the native Date thing
Date = function (string) {
var newDateString;
var plainDateRe = /[0-9]{4}-[0-9]{2}-[0-9]{2}$/;
if (string.match(plainDateRe) != null) {
newDateString = string + ' 00:00:00 GMT';
} else {
newDateString = string;
}
return new _Date(newDateString);
}
// Test:
var d = new Date('2015-10-10');
d.toString(); // Should output local time
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment