Skip to content

Instantly share code, notes, and snippets.

@davidmfoley
Created January 29, 2015 16:17
Show Gist options
  • Save davidmfoley/1aa5f8060582c4ccd271 to your computer and use it in GitHub Desktop.
Save davidmfoley/1aa5f8060582c4ccd271 to your computer and use it in GitHub Desktop.
Very kludgy PG timestamp to JS date
module.exports = {
// turns a postgres timestamptz field into a JS date
parse: function(s) {
//split on second fraction
var pieces = s.split('.');
// replace space with ISO T separator
pieces[0] = pieces[0].replace(' ', 'T');
// strip to milliseconds
pieces[1] = pieces[1].slice(0, 3) + pieces[1].slice(6);
// add minutes to TZ offset - doesn't handle non-hour TZs
pieces[1] = pieces[1] + ':00';
return new Date(pieces.join('.'));
}
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment