date.format.js 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. (function() {
  2. Date.shortMonths = ['Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec'];
  3. Date.longMonths = ['January', 'February', 'March', 'April', 'May', 'June', 'July', 'August', 'September', 'October', 'November', 'December'];
  4. Date.shortDays = ['Sun', 'Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat'];
  5. Date.longDays = ['Sunday', 'Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday'];
  6. // defining patterns
  7. var replaceChars = {
  8. // Day
  9. d: function() { return (this.getDate() < 10 ? '0' : '') + this.getDate(); },
  10. D: function() { return Date.shortDays[this.getDay()]; },
  11. j: function() { return this.getDate(); },
  12. l: function() { return Date.longDays[this.getDay()]; },
  13. N: function() { return (this.getDay() == 0 ? 7 : this.getDay()); },
  14. S: function() { return (this.getDate() % 10 == 1 && this.getDate() != 11 ? 'st' : (this.getDate() % 10 == 2 && this.getDate() != 12 ? 'nd' : (this.getDate() % 10 == 3 && this.getDate() != 13 ? 'rd' : 'th'))); },
  15. w: function() { return this.getDay(); },
  16. z: function() { var d = new Date(this.getFullYear(),0,1); return Math.ceil((this - d) / 86400000); }, // Fixed now
  17. // Week
  18. W: function() {
  19. var target = new Date(this.valueOf());
  20. var dayNr = (this.getDay() + 6) % 7;
  21. target.setDate(target.getDate() - dayNr + 3);
  22. var firstThursday = target.valueOf();
  23. target.setMonth(0, 1);
  24. if (target.getDay() !== 4) {
  25. target.setMonth(0, 1 + ((4 - target.getDay()) + 7) % 7);
  26. }
  27. return 1 + Math.ceil((firstThursday - target) / 604800000);
  28. },
  29. // Month
  30. F: function() { return Date.longMonths[this.getMonth()]; },
  31. m: function() { return (this.getMonth() < 9 ? '0' : '') + (this.getMonth() + 1); },
  32. M: function() { return Date.shortMonths[this.getMonth()]; },
  33. n: function() { return this.getMonth() + 1; },
  34. t: function() {
  35. var year = this.getFullYear(), nextMonth = this.getMonth() + 1;
  36. if (nextMonth === 12) {
  37. year = year++;
  38. nextMonth = 0;
  39. }
  40. return new Date(year, nextMonth, 0).getDate();
  41. },
  42. // Year
  43. L: function() { var year = this.getFullYear(); return (year % 400 == 0 || (year % 100 != 0 && year % 4 == 0)); }, // Fixed now
  44. o: function() { var d = new Date(this.valueOf()); d.setDate(d.getDate() - ((this.getDay() + 6) % 7) + 3); return d.getFullYear();}, //Fixed now
  45. Y: function() { return this.getFullYear(); },
  46. y: function() { return ('' + this.getFullYear()).substr(2); },
  47. // Time
  48. a: function() { return this.getHours() < 12 ? 'am' : 'pm'; },
  49. A: function() { return this.getHours() < 12 ? 'AM' : 'PM'; },
  50. B: function() { return Math.floor((((this.getUTCHours() + 1) % 24) + this.getUTCMinutes() / 60 + this.getUTCSeconds() / 3600) * 1000 / 24); }, // Fixed now
  51. g: function() { return this.getHours() % 12 || 12; },
  52. G: function() { return this.getHours(); },
  53. h: function() { return ((this.getHours() % 12 || 12) < 10 ? '0' : '') + (this.getHours() % 12 || 12); },
  54. H: function() { return (this.getHours() < 10 ? '0' : '') + this.getHours(); },
  55. i: function() { return (this.getMinutes() < 10 ? '0' : '') + this.getMinutes(); },
  56. s: function() { return (this.getSeconds() < 10 ? '0' : '') + this.getSeconds(); },
  57. u: function() { var m = this.getMilliseconds(); return (m < 10 ? '00' : (m < 100 ?
  58. '0' : '')) + m; },
  59. // Timezone
  60. e: function() { return /\((.*)\)/.exec(new Date().toString())[1]; },
  61. I: function() {
  62. var DST = null;
  63. for (var i = 0; i < 12; ++i) {
  64. var d = new Date(this.getFullYear(), i, 1);
  65. var offset = d.getTimezoneOffset();
  66. if (DST === null) DST = offset;
  67. else if (offset < DST) { DST = offset; break; } else if (offset > DST) break;
  68. }
  69. return (this.getTimezoneOffset() == DST) | 0;
  70. },
  71. O: function() { return (-this.getTimezoneOffset() < 0 ? '-' : '+') + (Math.abs(this.getTimezoneOffset() / 60) < 10 ? '0' : '') + (Math.abs(this.getTimezoneOffset() / 60)) + '00'; },
  72. P: function() { return (-this.getTimezoneOffset() < 0 ? '-' : '+') + (Math.abs(this.getTimezoneOffset() / 60) < 10 ? '0' : '') + (Math.abs(this.getTimezoneOffset() / 60)) + ':00'; }, // Fixed now
  73. T: function() { return this.toTimeString().replace(/^.+ \(?([^\)]+)\)?$/, '$1'); },
  74. Z: function() { return -this.getTimezoneOffset() * 60; },
  75. // Full Date/Time
  76. c: function() { return this.format("Y-m-d\\TH:i:sP"); }, // Fixed now
  77. r: function() { return this.toString(); },
  78. U: function() { return this.getTime() / 1000; }
  79. };
  80. // Simulates PHP's date function
  81. Date.prototype.format = function(format) {
  82. var date = this;
  83. return format.replace(/(\\?)(.)/g, function(_, esc, chr) {
  84. return (esc === '' && replaceChars[chr]) ? replaceChars[chr].call(date) : chr;
  85. });
  86. };
  87. }).call(this);