Skip to content

Instantly share code, notes, and snippets.

@huttj
Created January 13, 2015 09:26
Show Gist options
  • Save huttj/013ed9f63bc07de4fa00 to your computer and use it in GitHub Desktop.
Save huttj/013ed9f63bc07de4fa00 to your computer and use it in GitHub Desktop.
A simple method to parse a PostgreSQL time stamp in Safari.
// Returns the timestamp as a Date object (from the host's timezone),
// in GMT. If timestamp is *not* GMT, pass in a truthy value for isLocal.
function parseTimestamp(timestamp, isLocal) {
var z = isLocal ? 0 : new Date().getTimezoneOffset() / 60;
var t = timestamp.split(/[\- :\.]/).map(Number);
return new Date(t[0], t[1]-1, t[2], t[3]-z, t[4], t[5], t[6], t[7]);
}
var input = "2015-01-13 08:52:05.1234";
console.log(parseTimestamp(input).toString()); // 'Tue Jan 13 2015 00:52:06 GMT-0800 (Pacific Standard Time)'
console.log(parseTimestamp(input, true).toString()); // 'Tue Jan 13 2015 08:52:06 GMT-0800 (Pacific Standard Time)'
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment