Created
May 30, 2016 17:02
-
-
Save sylvainpolletvillard/6f5d194258f74cb8a3f58826da6f8770 to your computer and use it in GitHub Desktop.
Basic date formatting helper
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
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