Skip to content

Instantly share code, notes, and snippets.

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 sylvainpolletvillard/6f5d194258f74cb8a3f58826da6f8770 to your computer and use it in GitHub Desktop.
Save sylvainpolletvillard/6f5d194258f74cb8a3f58826da6f8770 to your computer and use it in GitHub Desktop.
Basic date formatting helper
function formatDate(value, format) {
var date = value && value instanceof Date ? value : new Date(value);
return format
.replace("YYYY", date.getFullYear().toString())
.replace("YY", date.getFullYear().toString().substr(2, 2))
.replace("MM", (date.getMonth() + 1).toString().replace(/^(\d)$/, '0$1'))
.replace("M", (date.getMonth() + 1).toString())
.replace("DD", date.getDate().toString().replace(/^(\d)$/, '0$1'))
.replace("D", date.getDate().toString())
.replace("HH", date.getHours().toString().replace(/^(\d)$/, '0$1'))
.replace("H", date.getHours().toString())
.replace("hh", ((date.getHours()%12)||12).toString().replace(/^(\d)$/, '0$1'))
.replace("h", ((date.getHours()%12)||12).toString())
.replace("mm", date.getMinutes().toString().replace(/^(\d)$/, '0$1'))
.replace("m", date.getMinutes().toString())
.replace("ss", date.getSeconds().toString().replace(/^(\d)$/, '0$1'))
.replace("s", date.getSeconds().toString())
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment