/ Published in: JavaScript
I don't really know how other people do this kind of thing, but this was my take.
Expand |
Embed | Plain Text
Copy this code and paste it in your HTML
//Test It formatDate(new Date().getTime(), 's/ss : m/mm : h/hh : D/DD : d/dd/ddd/ddd : M/MM/MMM/MMMM : yy/yyy : t/tt'); //Declare It function formatDate(dateInMs, formatString) { if(typeof dateInMs == 'number' && typeof formatString == 'string') { var dateObj = new Date(parseInt(dateInMs)); var dateString = ''; var dayArray = ['Sunday', 'Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday']; var monthArray = ['January', 'February', 'March', 'April', 'May', 'June', 'July', 'August', 'September', 'October', 'November', 'December']; for(var i = 0; i < formatString.length; i++) { switch(formatString[i]) { case 's': //'ss' for leading zero if(formatString[i+1] == 's') { dateString += (dateObj.getSeconds() >= 10 ? '' : '0') + dateObj.getSeconds(); i++; } else { dateString += dateObj.getSeconds(); } break; case 'm': //'mm' for leading zero if(formatString[i + 1] == 'm') { dateString += (dateObj.getMinutes() >= 10 ? '' : '0') + dateObj.getMinutes(); i++; } else { dateString += dateObj.getMinutes(); } break; case 'h': //'hh' for leading zero var military = dateObj.getHours(); var standard = military > 12 ? military-12 : military; if(formatString[i + 1] == 'h') { standard = (standard >= 10 ? '' : '0') + standard; i++; } dateString += standard; break; case 'D': // This is for day of month //'DD' for leading zero if(formatString[i + 1] == 'D') { dateString += (dateObj.getDate() >= 10 ? '' : '0') + dateObj.getDate(); i++; } else { dateString += dateObj.getDate(); } break; case 'd': //This is for day of week //'dd' for leading zero, 'ddd' for abbreviated day name, 'dddd' for full day name if(formatString[i + 1] == 'd' && formatString[i + 2] == 'd' && formatString[i + 3] == 'd') { dateString += dayArray[dateObj.getDay()]; i = i + 3; } else if(formatString[i + 1] == 'd' && formatString[i + 2] == 'd') { dateString += dayArray[dateObj.getDay()].substr(0, 3); i = i + 2; } else if(formatString[i + 1] == 'd') { dateString += '0' + dateObj.getDay(); i++; } else { dateString += dateObj.getDay(); } break; case 'M': //'MM' for leading zero, 'MMM' for abbreviated month name, 'MMMM' for full month name if(formatString[i + 1] == 'M' && formatString[i + 2] == 'M' && formatString[i + 3] == 'M') { dateString += monthArray[dateObj.getMonth()]; i = i + 3; } else if(formatString[i + 1] == 'M' && formatString[i + 2] == 'M') { dateString += monthArray[dateObj.getMonth()].substr(0, 3); i = i + 2; } else if(formatString[i + 1] == 'M') { dateString += (dateObj.getMonth() >= 10 ? '' : '0') + dateObj.getMonth(); i++; } else { dateString += dateObj.getMonth(); } break; case 'y': //'yy' for two-digit number, 'yyyy' for four-digit number if(formatString[i + 1] == 'y' && formatString[i + 2] == 'y' && formatString[i + 3] == 'y') { dateString += dateObj.getFullYear(); i = i + 3; } else if(formatString[i + 1] == 'y') { dateString += String(dateObj.getFullYear()).substr(2, 2); i++; } else { dateString += formatString[i] } break; case 't': //'t' first character of am/pm, 'tt' for both characters of am/pm var military = dateObj.getHours(); var ampm = military > 12 ? 'pm' : 'am'; if(formatString[i + 1] == 't') { dateString += ampm; i++; } else { dateString += ampm.substr(0, 1); } break; default: dateString += formatString[i] } } return dateString; } else { console.error('Invalid date or formatString provided to formatDate(). Try sending time in milliseconds and a format string.'); return ''; } }