Skip to content

Instantly share code, notes, and snippets.

@seanrankin
Created August 21, 2019 16:52
Show Gist options
  • Select an option

  • Save seanrankin/1515a67166f1cba4c1e9c5f39db351f1 to your computer and use it in GitHub Desktop.

Select an option

Save seanrankin/1515a67166f1cba4c1e9c5f39db351f1 to your computer and use it in GitHub Desktop.
formatDateToString.js
function formatDateToString(date, format){
const newDate = new Date(date);
const D = newDate.getDate();
const DD = (newDate.getDate() < 10 ? '0' : '') + newDate.getDate();
const M = newDate.getMonth() + 1;
const MM = ((newDate.getMonth() + 1) < 10 ? '0' : '') + (newDate.getMonth() + 1);
const YYYY = newDate.getFullYear();
switch (format) {
// Used in spreadsheet
case "M/D/YYY":
return (M + "/" + D + "/" + YYYY);
break;
// Used in the IGMS API
case "YYYY-MM-DD":
return (YYYY + "-" + MM + "-" + DD);
break;
default:
return (M + "/" + D + "/" + YYYY);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment