Created
October 16, 2015 22:44
-
-
Save jaromero/f4975880abecc522a2a9 to your computer and use it in GitHub Desktop.
Kludgey monkey-patched ES6 UTC-enabled Date()
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
// 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