- /*
- date.js: Useful extensions to the JavaScript Date object. Copyright (C)
- 1999-2003 Jan Wessely <mailto:info@jawe.net>
- Permission is hereby granted, free of charge, to any person obtaining a copy of
- this software and associated documentation files (the "Software"), to deal in
- the Software without restriction, including without limitation the rights to
- use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
- the Software, and to permit persons to whom the Software is furnished to do so,
- subject to the following conditions:
- The above copyright notice and this permission notice shall be included in all
- copies or substantial portions of the Software.
- THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
- IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
- FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
- COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
- IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
- CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
- created: 25 June 1998
- last modified: 17 Jan 2000
- */
- // literals *******************************************************************
- // used as param unit in Date.add()
- Date.MILLI = 1;
- Date.SECOND = Date.MILLI * 1000;
- Date.MINUTE = Date.SECOND * 60;
- Date.HOUR = Date.MINUTE * 60;
- Date.DAY = Date.HOUR * 24;
- Date.MONTH = -1;
- Date.YEAR = -2;
- Date.DAYS_IN_MONTH = new Array(31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31);
- Date.CENTURY = 21;
- // methods ********************************************************************
- function _Date_toCanonString() {
- return this.getFullYear() +
- _pad(this.getMonth() + 1) +
- _pad(this.getDate());
- }
- function _Date_getFullYear() {
- var y = this.getYear();
- if(y < 100 && y > 0) {
- y += (Date.CENTURY - 1) * 1000;
- }
- return y;
- }
- function _Date_setFullYear(val) {
- this.setYear(val);
- }
- function _Date_compareTo(other) {
- return Date.compare(this, other);
- }
- function _Date_isLeapYear() {
- return Date.leapYear(this.getFullYear());
- }
- function _Date_add(date, unit, amount) {
- return Date.addDate(this, date, unit, amount);
- }
- function _Date_getDaysInMonth() {
- return Date.daysInMonth(this.getFullYear(), this.getMonth());
- }
- function _Date_getDayInYear() {
- return Date.dayInYear(this.getFullYear(), this.getDate());
- }
- // utility functions **********************************************************
- function _isLeapYear(year) {
- return (year % 4 == 0 && year % 100 != 0) || year % 400 == 0;
- }
- function _compareDate(d1, d2) {
- return (new Date(d1)).getTime() - (new Date(d2)).getTime();
- }
- function _addDate(date, unit, amount) {
- if(unit == Date.MONTH)
- date.setMonth(date.getMonth() + amount);
- else if(unit == Date.YEAR)
- date.setFullYear(date.getFullYear() + amount);
- else
- date.setTime(date.getTime() + unit * amount);
- return date;
- }
- function _getDaysInMonth(year, month) {
- return month == 1 && Date.leapYear(year) ? 29 : Date.DAYS_IN_MONTH[month];
- }
- function _getDayInYear(date) {
- var year = date.getFullYear();
- var days = date.getDate();
- for(var month = 0; month < date.getMonth(); month++) {
- days += Date.daysInMonth(year, month);
- }
- return days;
- }
- function _getWeek(date, hold) {
- var firstWeekDay = new Date(date.getFullYear(), 1, 1).getDay();
- var o;
- if ( firstWeekDay > 0 && firstWeekDay < 5)
- o = firstWeekDay - 1;
- else if(firstWeekDay == 5)
- firstWeekDay = -3;
- else if (firstWeekDay == 6)
- o = -2;
- else
- o = -1;
- var nr = Math.ceil((o + date.getDayInYear() ) / 7);
- if(hold || nr)
- return nr;
- nr = new Date(date.getFullYear() - 1, 12, 31);
- return nr.getWeek(true);
- }
- function _pad(n) {
- return (n < 10 ? "0" : "") + n;
- }
- // initialization *************************************************************
- Date.prototype.toCanonString = _Date_toCanonString;
- if(!Date.prototype.getFullYear) {
- Date.prototype.getFullYear = _Date_getFullYear;
- Date.prototype.setFullYear = _Date_setFullYear;
- }
- Date.prototype.isLeapYear = _Date_isLeapYear;
- Date.prototype.compareTo = _Date_compareTo;
- Date.prototype.add = _Date_add;
- Date.prototype.getDaysInMonth = _Date_getDaysInMonth;
- Date.prototype.getDayInYear = _Date_getDayInYear;
- Date.leapYear = _isLeapYear;
- Date.compare = _compareDate;
- Date.addDate = _addDate;
- Date.daysInMonth = _getDaysInMonth;
- Date.dayInYear = _getDayInYear;