Created
August 13, 2011 05:53
-
-
Save chrisdickinson/1143519 to your computer and use it in GitHub Desktop.
Date#toString with provided UTC offset.
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
var ofs = function(s) { | |
var minute_ofs = 1000*60*parseInt(s.slice(-2), 10) | |
, hour_ofs = 1000*60*60*parseInt(s.slice(-4,3), 10) | |
, sign = s.charAt(0) === '+' ? 1 : -1 | |
return sign * (minute_ofs + hour_ofs) | |
} | |
Date.prototype.toString = function(ts, offset) { | |
offset === undefined ? | |
(offset = ts, ts = +this): | |
null | |
offset = offset || '+0000' | |
ts = ts || +this | |
var d = new Date(ts + ofs(offset)) | |
return d.toUTCString().slice(0, -4)+' '+offset | |
} | |
console.log(''+new Date()) | |
console.log(new Date().toString('-0500')) | |
console.log(new Date().toString('-0600')) | |
console.log(new Date().toString('-0700')) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment