Created
June 4, 2015 13:27
-
-
Save jasdeepkhalsa/4f31888b4e0b1103f333 to your computer and use it in GitHub Desktop.
Format Date
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
// From: http://stackoverflow.com/questions/2315408/how-do-i-format-a-timestamp-in-javascript-to-display-it-in-graphs-utc-is-fine | |
function formatDate(date, fmt) { | |
function pad(value) { | |
return (value.toString().length < 2) ? '0' + value : value; | |
} | |
return fmt.replace(/%([a-zA-Z])/g, function (_, fmtCode) { | |
switch (fmtCode) { | |
case 'Y': | |
return date.getUTCFullYear(); | |
case 'M': | |
return pad(date.getUTCMonth() + 1); | |
case 'd': | |
return pad(date.getUTCDate()); | |
case 'H': | |
return pad(date.getUTCHours()); | |
case 'm': | |
return pad(date.getUTCMinutes()); | |
case 's': | |
return pad(date.getUTCSeconds()); | |
default: | |
throw new Error('Unsupported format code: ' + fmtCode); | |
} | |
}); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment