Created
April 24, 2015 14:11
-
-
Save jczaplew/f055788bf851d0840f50 to your computer and use it in GitHub Desktop.
Javascript Date to Postgres-acceptable format
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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
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','');