Guest User

Untitled

a guest
Dec 14th, 2018
119
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.95 KB | None | 0 0
  1. function formatDate(date,format) {
  2. /**
  3. *
  4. * ex: formatDate(new date(), 'l, F S @ g:ia')
  5. * -> Tuesday, January 3rd @ 2:30pm
  6. *
  7. */
  8. format = (format ? format : 'F d');
  9. var unformatted = new Date(date);
  10.  
  11. function addZero(n) {
  12. return n<10 ? '0'+n : n;
  13. }
  14.  
  15.  
  16. function ordinal(d) {
  17. if(d>3 && d<21) return 'th';
  18. switch (d % 10) {
  19. case 1: return "st";
  20. case 2: return "nd";
  21. case 3: return "rd";
  22. default: return "th";
  23. }
  24. }
  25.  
  26. function ampm(h){
  27. return (h >= 12 ? "PM" : "AM");
  28. }
  29.  
  30. function twelveHour(h){
  31. return (h > 12 ? (h-12) : ((h == 0 ? 12 : h)));
  32. }
  33.  
  34.  
  35.  
  36. var days_full = ['Sunday','Monday','Tuesday','Wednesday','Thursday','Friday','Saturday'];
  37. var days_3only = ['Sun','Mon','Tue','Wed','Thu','Fri','Sat'];
  38. var month_full = ["January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"];
  39. var month_3only = ["Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"];
  40.  
  41. var date_constructs = {
  42.  
  43. /**
  44. *
  45. * Time
  46. *
  47. */
  48. // 12-hour format without leading zeroes
  49. 'g' : twelveHour(unformatted.getHours()),
  50.  
  51. // 23-hour format without leading zeroes
  52. 'G' : unformatted.getHours(),
  53.  
  54. // 12-hour format with leading zeroes
  55. 'h' : addZero(twelveHour(unformatted.getHours())),
  56.  
  57. // 23-hour format with leading zeroes
  58. 'H' : addZero(unformatted.getHours()),
  59.  
  60. // minutes with leading zeroes
  61. 'i' : addZero(unformatted.getMinutes()),
  62.  
  63. // am or pm
  64. 'a' : ampm(unformatted.getHours()).toLowerCase(),
  65.  
  66. // AM or PM
  67. 'A' : ampm(unformatted.getHours()),
  68.  
  69.  
  70. /**
  71. *
  72. * Days
  73. *
  74. */
  75.  
  76. // day of the week (number) - 0 is Sunday, 6 is Saturday
  77. 'w' : unformatted.getDay(),
  78.  
  79. // day of the week (text, full) (Monday)
  80. 'l' : days_full[unformatted.getDay()],
  81.  
  82. // day of the week (text, 3 letters) (Mon)
  83. 'D' : days_3only[unformatted.getDay()],
  84.  
  85. // day of the month. 2 digits (01)
  86. 'd' : addZero(unformatted.getDate()),
  87.  
  88. // day of the month. no leading zero (1)
  89. 'j' : unformatted.getDate(),
  90.  
  91. // day of the month, ordinal. no leading zero, with suffix. (3rd)
  92. 'S' : unformatted.getDate() + ordinal(unformatted.getDate()),
  93.  
  94. /**
  95. *
  96. * Months
  97. *
  98. */
  99.  
  100. // month as names (January)
  101. 'F' : month_full[unformatted.getMonth()],
  102.  
  103. // month - numeric with leading zeros (01)
  104. 'm' : addZero(unformatted.getMonth() + 1),
  105.  
  106. // month - numeries without leading zeros (1)
  107. 'n' : (unformatted.getMonth() + 1),
  108.  
  109. // month (text, 3 letters) (Jan)
  110. 'M' : month_3only[unformatted.getMonth()],
  111.  
  112. /**
  113. *
  114. * Year
  115. *
  116. */
  117.  
  118. // year (4 digits)
  119. 'Y' : unformatted.getFullYear(),
  120.  
  121. // year (2 digits)
  122. 'y' : unformatted.getFullYear().toString().substr(-2),
  123.  
  124. };
  125.  
  126. var formatted = '';
  127. for (var i = 0; i <= format.length - 1; i++) {
  128. if(date_constructs[format[i]]===undefined){
  129. formatted+=format[i];
  130. } else {
  131. formatted+=date_constructs[format[i]];
  132. }
  133. }
  134.  
  135.  
  136. return formatted;
  137.  
  138. }
Add Comment
Please, Sign In to add comment