Skip to content

Instantly share code, notes, and snippets.

@robsmith1776
Forked from jczaplew/pgFormatDate.js
Created August 11, 2022 01:59
Show Gist options
  • Save robsmith1776/0ae0a1ec505f54fc7365267eb5c69eee to your computer and use it in GitHub Desktop.
Save robsmith1776/0ae0a1ec505f54fc7365267eb5c69eee 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(" ");
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment