JavaScript Date Formatting


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

Format a date into a string using several string variables.

Usage:
(new Date("6/6/2011")).format("%W, %B %d%o, %Y");

Result:
Monday, June 6th, 2011


Copy this code and paste it in your HTML
  1. /* Date formatting v2.0
  2.  *
  3.  * This work is licensed under a Creative Commons Attribution 3.0 Unported License
  4.  * http://creativecommons.org/licenses/by/3.0/
  5.  *
  6.  * Author: Andy Harrison, http://dragonzreef.com/
  7.  * Date: 16 September 2011
  8.  */
  9.  
  10. Date.prototype.getMonthName = function(){ return (["January","February","March","April","May","June","July","August","September","October","November","December"])[this.getMonth()]; }
  11. Date.prototype.getUTCMonthName = function(){ return (["January","February","March","April","May","June","July","August","September","October","November","December"])[this.getUTCMonth()]; }
  12. Date.prototype.getDayName = function(){ return (["Sunday","Monday","Tuesday","Wednesday","Thursday","Friday","Saturday"])[this.getDay()]; }
  13. Date.prototype.getUTCDayName = function(){ return (["Sunday","Monday","Tuesday","Wednesday","Thursday","Friday","Saturday"])[this.getUTCDay()]; }
  14.  
  15. //if useUTC is true, UTC values will be used instead of local time values
  16. Date.prototype.format = function(formatStr, useUTC)
  17. {
  18. /*
  19. Format string variables:
  20.  
  21. %Y 4-digit year (e.g., 2011)
  22. %y 2-digit year (e.g., 11)
  23. %M 2-digit month (01-12)
  24. %m month (1-12)
  25. %B full month name (January-December)
  26. %b abbreviated month name (Jan-Dec)
  27. %D 2-digit day of month (01-31)
  28. %d day of month (1-31)
  29. %o ordinal of the day of month (st, nd, rd, th)
  30. %W full weekday name (Sunday-Saturday)
  31. %w abbreviated weekday name (Sun-Sat)
  32. %I hour in 24-hour format (00-23)
  33. %H 2-digit hour in 12-hour format (01-12)
  34. %h hour in 12-hour format (1-12)
  35. %P AM/PM
  36. %p am/pm
  37. %q a/p
  38. %N 2-digit minute (00-59)
  39. %n minute (0-59)
  40. %S 2-digit second (00-59)
  41. %s second (0-59)
  42. %Z 3-digit milliseconds (000-999)
  43. %z milliseconds (0-999)
  44. %e UTC offset +/-
  45. %F 2-digit hour offset (00-23)
  46. %f hour offset (0-23)
  47. %G 2-digit minute offset (00-59)
  48. %g minute offset (0-59)
  49.  
  50. %% percent sign
  51. */
  52.  
  53. function pad(numStr, digits)
  54. {
  55. numStr = numStr.toString();
  56. while(numStr.length < digits) numStr = "0"+numStr;
  57. return numStr;
  58. }
  59.  
  60. var theYear = useUTC ? this.getUTCFullYear() : this.getFullYear();
  61. var theMonth = useUTC ? this.getUTCMonth() : this.getMonth();
  62. var theMonthName = useUTC ? this.getUTCMonthName() : this.getMonthName();
  63. var theDate = useUTC ? this.getUTCDate() : this.getDate();
  64. var theDayName = useUTC ? this.getUTCDayName() : this.getDayName();
  65. var theHour = useUTC ? this.getUTCHours() : this.getHours();
  66. var theMinute = useUTC ? this.getUTCMinutes() : this.getMinutes();
  67. var theSecond = useUTC ? this.getUTCSeconds() : this.getSeconds();
  68. var theMS = useUTC ? this.getUTCMilliseconds() : this.getMilliseconds();
  69. var theOffset = useUTC ? 0 : -this.getTimezoneOffset(); //offset in minutes
  70.  
  71. var v = /%(.)/, m, formatted = "", d, h;
  72. while((m = v.exec(formatStr)) != null)
  73. {
  74. formatted += formatStr.slice(0, m.index);
  75. switch(m[1])
  76. {
  77. case "Y": formatted += theYear; break;
  78. case "y": formatted += theYear.toString().slice(-2); break;
  79. case "M": formatted += pad(theMonth+1, 2); break;
  80. case "m": formatted += theMonth+1; break;
  81. case "B": formatted += theMonthName; break;
  82. case "b": formatted += theMonthName.slice(0,3); break;
  83. case "D": formatted += pad(theDate, 2); break;
  84. case "d": formatted += theDate; break;
  85. case "o":
  86. d = theDate;
  87. formatted += (d==1 || d==21 || d==31) ? "st" : (d==2 || d==22) ? "nd" : (d==3 || d==23) ? "rd" : "th";
  88. break;
  89. case "W": formatted += theDayName; break;
  90. case "w": formatted += theDayName.slice(0,3); break;
  91. case "I": formatted += pad(theHour, 2); break;
  92. case "H":
  93. h = theHour % 12;
  94. if(h==0) h = 12;
  95. formatted += pad(h, 2);
  96. break;
  97. case "h":
  98. h = theHour % 12;
  99. if(h==0) h = 12;
  100. formatted += h;
  101. break;
  102. case "P": formatted += (theHour<12 ? "AM" : "PM"); break;
  103. case "p": formatted += (theHour<12 ? "am" : "pm"); break;
  104. case "q": formatted += (theHour<12 ? "a" : "p"); break;
  105. case "N": formatted += pad(theMinute, 2); break;
  106. case "n": formatted += theMinute; break;
  107. case "S": formatted += pad(theSecond, 2); break;
  108. case "s": formatted += theSecond; break;
  109. case "Z": formatted += pad(theMS, 3); break;
  110. case "z": formatted += theMS; break;
  111. case "e": formatted += theOffset < 0 ? "-" : "+"; break; //if offset==0, it will be "+"
  112. case "F": formatted += pad(Math.floor(Math.abs(theOffset)/60), 2); break;
  113. case "f": formatted += Math.floor(Math.abs(theOffset)/60); break;
  114. case "G": formatted += pad(theOffset%60, 2); break;
  115. case "g": formatted += theOffset%60; break;
  116. case "%": formatted += "%"; break;
  117. default: formatted += m[0];
  118. }
  119. formatStr = formatStr.slice(m.index+2);
  120. }
  121. formatted += formatStr;
  122.  
  123. return formatted;
  124. }

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.