Skip to content

Instantly share code, notes, and snippets.

@chrisdickinson
Created August 13, 2011 05:53
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save chrisdickinson/1143519 to your computer and use it in GitHub Desktop.
Save chrisdickinson/1143519 to your computer and use it in GitHub Desktop.
Date#toString with provided UTC offset.
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