Skip to content

Instantly share code, notes, and snippets.

@jczaplew
Created April 24, 2015 14:11
Show Gist options
  • Save jczaplew/f055788bf851d0840f50 to your computer and use it in GitHub Desktop.
Save jczaplew/f055788bf851d0840f50 to your computer and use it in GitHub Desktop.
Javascript Date to Postgres-acceptable format
// Convert Javascript date to Pg YYYY MM DD HH MI SS
function pgFormatDate(date) {
/* Via http://stackoverflow.com/questions/3605214/javascript-add-leading-zeroes-to-date */
function zeroPad(d) {
return ("0" + d).slice(-2)
}
var parsed = new Date(date)
return [parsed.getUTCFullYear(), zeroPad(parsed.getMonth() + 1), zeroPad(parsed.getDate()), zeroPad(parsed.getHours()), zeroPad(parsed.getMinutes()), zeroPad(parsed.getSeconds())].join(" ");
}
@mhechthz
Copy link

mhechthz commented Apr 2, 2020

time = new Date(Date.now()).toISOString().replace('T',' ').replace('Z','');
or with timezone correction
time = new Date(Date.now()+(1000*60*(-(new Date()).getTimezoneOffset()))).toISOString().replace('T',' ').replace('Z','');

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment