1. /*
  2. date.js: Useful extensions to the JavaScript Date object. Copyright (C)
  3. 1999-2003 Jan Wessely <mailto:info@jawe.net>
  4.  
  5. Permission is hereby granted, free of charge, to any person obtaining a copy of
  6. this software and associated documentation files (the "Software"), to deal in
  7. the Software without restriction, including without limitation the rights to
  8. use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
  9. the Software, and to permit persons to whom the Software is furnished to do so,
  10. subject to the following conditions:
  11.  
  12. The above copyright notice and this permission notice shall be included in all
  13. copies or substantial portions of the Software.
  14.  
  15. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  16. IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
  17. FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
  18. COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
  19. IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
  20. CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  21.  
  22. created: 25 June 1998
  23. last modified: 17 Jan 2000
  24. */
  25.  
  26. // literals *******************************************************************
  27.  
  28. // used as param unit in Date.add()
  29. Date.MILLI = 1;
  30. Date.SECOND = Date.MILLI * 1000;
  31. Date.MINUTE = Date.SECOND * 60;
  32. Date.HOUR = Date.MINUTE * 60;
  33. Date.DAY = Date.HOUR * 24;
  34. Date.MONTH = -1;
  35. Date.YEAR = -2;
  36.  
  37. Date.DAYS_IN_MONTH = new Array(31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31);
  38. Date.CENTURY = 21;
  39.  
  40. // methods ********************************************************************
  41.  
  42. function _Date_toCanonString() {
  43. return this.getFullYear() +
  44. _pad(this.getMonth() + 1) +
  45. _pad(this.getDate());
  46. }
  47.  
  48. function _Date_getFullYear() {
  49. var y = this.getYear();
  50. if(y < 100 && y > 0) {
  51. y += (Date.CENTURY - 1) * 1000;
  52. }
  53. return y;
  54. }
  55.  
  56. function _Date_setFullYear(val) {
  57. this.setYear(val);
  58. }
  59.  
  60. function _Date_compareTo(other) {
  61. return Date.compare(this, other);
  62. }
  63.  
  64. function _Date_isLeapYear() {
  65. return Date.leapYear(this.getFullYear());
  66. }
  67.  
  68. function _Date_add(date, unit, amount) {
  69. return Date.addDate(this, date, unit, amount);
  70. }
  71.  
  72. function _Date_getDaysInMonth() {
  73. return Date.daysInMonth(this.getFullYear(), this.getMonth());
  74. }
  75.  
  76. function _Date_getDayInYear() {
  77. return Date.dayInYear(this.getFullYear(), this.getDate());
  78. }
  79.  
  80. // utility functions **********************************************************
  81.  
  82. function _isLeapYear(year) {
  83. return (year % 4 == 0 && year % 100 != 0) || year % 400 == 0;
  84. }
  85.  
  86. function _compareDate(d1, d2) {
  87. return (new Date(d1)).getTime() - (new Date(d2)).getTime();
  88. }
  89.  
  90. function _addDate(date, unit, amount) {
  91. if(unit == Date.MONTH)
  92. date.setMonth(date.getMonth() + amount);
  93. else if(unit == Date.YEAR)
  94. date.setFullYear(date.getFullYear() + amount);
  95. else
  96. date.setTime(date.getTime() + unit * amount);
  97. return date;
  98. }
  99.  
  100. function _getDaysInMonth(year, month) {
  101. return month == 1 && Date.leapYear(year) ? 29 : Date.DAYS_IN_MONTH[month];
  102. }
  103.  
  104. function _getDayInYear(date) {
  105. var year = date.getFullYear();
  106. var days = date.getDate();
  107. for(var month = 0; month < date.getMonth(); month++) {
  108. days += Date.daysInMonth(year, month);
  109. }
  110. return days;
  111. }
  112.  
  113. function _getWeek(date, hold) {
  114. var firstWeekDay = new Date(date.getFullYear(), 1, 1).getDay();
  115. var o;
  116. if ( firstWeekDay > 0 && firstWeekDay < 5)
  117. o = firstWeekDay - 1;
  118. else if(firstWeekDay == 5)
  119. firstWeekDay = -3;
  120. else if (firstWeekDay == 6)
  121. o = -2;
  122. else
  123. o = -1;
  124. var nr = Math.ceil((o + date.getDayInYear() ) / 7);
  125. if(hold || nr)
  126. return nr;
  127. nr = new Date(date.getFullYear() - 1, 12, 31);
  128. return nr.getWeek(true);
  129. }
  130.  
  131. function _pad(n) {
  132. return (n < 10 ? "0" : "") + n;
  133. }
  134.  
  135. // initialization *************************************************************
  136.  
  137. Date.prototype.toCanonString = _Date_toCanonString;
  138. if(!Date.prototype.getFullYear) {
  139. Date.prototype.getFullYear = _Date_getFullYear;
  140. Date.prototype.setFullYear = _Date_setFullYear;
  141. }
  142. Date.prototype.isLeapYear = _Date_isLeapYear;
  143. Date.prototype.compareTo = _Date_compareTo;
  144. Date.prototype.add = _Date_add;
  145. Date.prototype.getDaysInMonth = _Date_getDaysInMonth;
  146. Date.prototype.getDayInYear = _Date_getDayInYear;
  147.  
  148. Date.leapYear = _isLeapYear;
  149. Date.compare = _compareDate;
  150. Date.addDate = _addDate;
  151. Date.daysInMonth = _getDaysInMonth;
  152. Date.dayInYear = _getDayInYear;