Light Date-to-String formatting


/ Published in: JavaScript
Save to your folder(s)

I don't really know how other people do this kind of thing, but this was my take.


Copy this code and paste it in your HTML
  1. //Test It
  2. formatDate(new Date().getTime(), 's/ss : m/mm : h/hh : D/DD : d/dd/ddd/ddd : M/MM/MMM/MMMM : yy/yyy : t/tt');
  3.  
  4. //Declare It
  5. function formatDate(dateInMs, formatString)
  6. {
  7. if(typeof dateInMs == 'number' && typeof formatString == 'string')
  8. {
  9. var dateObj = new Date(parseInt(dateInMs));
  10. var dateString = '';
  11. var dayArray = ['Sunday', 'Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday'];
  12. var monthArray = ['January', 'February', 'March', 'April', 'May', 'June', 'July', 'August', 'September', 'October', 'November', 'December'];
  13.  
  14. for(var i = 0; i < formatString.length; i++)
  15. {
  16.  
  17. switch(formatString[i])
  18. {
  19. case 's': //'ss' for leading zero
  20. if(formatString[i+1] == 's') {
  21. dateString += (dateObj.getSeconds() >= 10 ? '' : '0') + dateObj.getSeconds();
  22. i++;
  23. }
  24. else {
  25. dateString += dateObj.getSeconds();
  26. }
  27. break;
  28. case 'm': //'mm' for leading zero
  29. if(formatString[i + 1] == 'm') {
  30. dateString += (dateObj.getMinutes() >= 10 ? '' : '0') + dateObj.getMinutes();
  31. i++;
  32. }
  33. else {
  34. dateString += dateObj.getMinutes();
  35. }
  36. break;
  37. case 'h': //'hh' for leading zero
  38. var military = dateObj.getHours();
  39. var standard = military > 12 ? military-12 : military;
  40. if(formatString[i + 1] == 'h') {
  41. standard = (standard >= 10 ? '' : '0') + standard;
  42. i++;
  43. }
  44. dateString += standard;
  45. break;
  46. case 'D': // This is for day of month //'DD' for leading zero
  47. if(formatString[i + 1] == 'D') {
  48. dateString += (dateObj.getDate() >= 10 ? '' : '0') + dateObj.getDate();
  49. i++;
  50. }
  51. else {
  52. dateString += dateObj.getDate();
  53. }
  54. break;
  55. case 'd': //This is for day of week //'dd' for leading zero, 'ddd' for abbreviated day name, 'dddd' for full day name
  56. if(formatString[i + 1] == 'd' && formatString[i + 2] == 'd' && formatString[i + 3] == 'd') {
  57. dateString += dayArray[dateObj.getDay()];
  58. i = i + 3;
  59. }
  60. else if(formatString[i + 1] == 'd' && formatString[i + 2] == 'd') {
  61. dateString += dayArray[dateObj.getDay()].substr(0, 3);
  62. i = i + 2;
  63. }
  64. else if(formatString[i + 1] == 'd') {
  65. dateString += '0' + dateObj.getDay();
  66. i++;
  67. }
  68. else {
  69. dateString += dateObj.getDay();
  70. }
  71. break;
  72. case 'M': //'MM' for leading zero, 'MMM' for abbreviated month name, 'MMMM' for full month name
  73. if(formatString[i + 1] == 'M' && formatString[i + 2] == 'M' && formatString[i + 3] == 'M') {
  74. dateString += monthArray[dateObj.getMonth()];
  75. i = i + 3;
  76. }
  77. else if(formatString[i + 1] == 'M' && formatString[i + 2] == 'M') {
  78. dateString += monthArray[dateObj.getMonth()].substr(0, 3);
  79. i = i + 2;
  80. }
  81. else if(formatString[i + 1] == 'M') {
  82. dateString += (dateObj.getMonth() >= 10 ? '' : '0') + dateObj.getMonth();
  83. i++;
  84. }
  85. else {
  86. dateString += dateObj.getMonth();
  87. }
  88. break;
  89. case 'y': //'yy' for two-digit number, 'yyyy' for four-digit number
  90. if(formatString[i + 1] == 'y' && formatString[i + 2] == 'y' && formatString[i + 3] == 'y') {
  91. dateString += dateObj.getFullYear();
  92. i = i + 3;
  93. }
  94. else if(formatString[i + 1] == 'y') {
  95. dateString += String(dateObj.getFullYear()).substr(2, 2);
  96. i++;
  97. }
  98. else {
  99. dateString += formatString[i]
  100. }
  101. break;
  102. case 't': //'t' first character of am/pm, 'tt' for both characters of am/pm
  103. var military = dateObj.getHours();
  104. var ampm = military > 12 ? 'pm' : 'am';
  105.  
  106. if(formatString[i + 1] == 't') {
  107. dateString += ampm;
  108. i++;
  109. }
  110. else {
  111. dateString += ampm.substr(0, 1);
  112. }
  113. break;
  114. default:
  115. dateString += formatString[i]
  116. }
  117. }
  118.  
  119. return dateString;
  120. }
  121. else
  122. {
  123. console.error('Invalid date or formatString provided to formatDate(). Try sending time in milliseconds and a format string.');
  124. return '';
  125. }
  126. }

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.