Skip to content

Instantly share code, notes, and snippets.

@jonkemp
Last active August 29, 2015 13:56
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jonkemp/9257854 to your computer and use it in GitHub Desktop.
Save jonkemp/9257854 to your computer and use it in GitHub Desktop.
Take a date string and return it in a certain format.
function pad(num) {
return (num < 10 ? '0' : '') + num;
}
function formatDateMonthDayYear(date) {
if (date) {
date = new Date(date);
} else {
date = new Date();
}
return pad(date.getMonth() + 1) +
'/' + pad(date.getUTCDate()) +
'/' + date.getFullYear();
}
function toISOString(date) {
if (date) {
date = new Date(date);
} else {
date = new Date();
}
return date.getFullYear() +
'-' + pad(date.getMonth() + 1) +
'-' + pad(date.getDate()) +
'T' + pad(date.getHours()) +
':' + pad(date.getMinutes()) +
':' + pad(date.getSeconds());
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment