Skip to content

Instantly share code, notes, and snippets.

@fredyfx
Created March 8, 2017 00:50
Show Gist options
  • Save fredyfx/676d17ac566d02d7b2bb9132bb93acd6 to your computer and use it in GitHub Desktop.
Save fredyfx/676d17ac566d02d7b2bb9132bb93acd6 to your computer and use it in GitHub Desktop.
var date = new Date("01/21/2017");
var yyyy = date.getFullYear().toString();
var mm = (date.getMonth()+1).toString();
var dd = date.getDate().toString();
// CONVERT mm AND dd INTO chars
var mmChars = mm.split('');
var ddChars = dd.split('');
// CONCAT THE STRINGS IN YYYY-MM-DD FORMAT
var datestring = yyyy + '-' + (mmChars[1]?mm:"0"+mmChars[0]) + '-' + (ddChars[1]?dd:"0"+ddChars[0]);
console.log("the result is: "); //2017-01-21
console.log(datestring);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment