webgui/www/extras/yui/docs/calendar/overview-summary-DateMath.js.html
JT Smith 4f68a0933c added YUI and YUI-ext
fixed the resizable text area with IE problem
fixed the ad space with IE problem
merged the 7.2.0 and 7.1.4 change logs
2006-11-07 23:15:57 +00:00

482 lines
17 KiB
HTML

<html>
<head>
<title>API: Calendar overview-summary-DateMath.js.html (YUI Library)</title>
<link href="stylesheet.css" rel="stylesheet" type="text/css" />
</head>
<body>
<div id="header">
<h1>Yahoo! UI Library</h1>
<h3><a href="./index.html">Yahoo! UI Library: Calendar</a></h3>
<div class="breadcrumbs">
<a href="./index.html">Yahoo! UI Library: Calendar</a>
&gt;
<strong>DateMath.js</strong>
</div>
</div>
<div id="body">
<div class="nav">
<div class="module resources">
<ul class="content">
<li><a href="overview-tree.html">Tree View</a></li>
<li><a href="index-all.html">Element Index</a></li>
</ul>
</div>
<div class="module">
<h4><a href="./allclasses-noframe.html">Classes</a></h4>
<ul class="content">
<li>
<a href="YAHOO.widget.html">
YAHOO.widget</a>
</li>
<li>
<a href="YAHOO.widget.Calendar.html">
YAHOO.widget.Calendar</a>
</li>
<li>
<a href="YAHOO.widget.Calendar2up.html">
YAHOO.widget.Calendar2up</a>
</li>
<li>
<a href="YAHOO.widget.Calendar2up_Cal.html">
YAHOO.widget.Calendar2up_Cal</a>
</li>
<li>
<a href="YAHOO.widget.Calendar_Core.html">
YAHOO.widget.Calendar_Core</a>
</li>
<li>
<a href="YAHOO.widget.CalendarGroup.html">
YAHOO.widget.CalendarGroup</a>
</li>
<li>
<a href="YAHOO.widget.DateMath.html">
YAHOO.widget.DateMath</a>
</li>
</ul>
</div>
<div class="module">
<h4><a href="./overview-summary.html">Files</a></h4>
<ul class="content">
<li>
<a href="overview-summary-Calendar.js.html">
Calendar.js</a>
</li>
<li>
<a href="overview-summary-Calendar2up.js.html">
Calendar2up.js</a>
</li>
<li>
<a href="overview-summary-Calendar_Core.js.html">
Calendar_Core.js</a>
</li>
<li>
<a href="overview-summary-CalendarGroup.js.html">
CalendarGroup.js</a>
</li>
<li>
<a href="overview-summary-DateMath.js.html">
DateMath.js</a>
</li>
</ul>
</div>
</div>
<div class="main">
<h2>DateMath.js</h2>
<div class="meta">
</div>
<div class="section source">
<style>
strong.alert {color:red;}
</style>
<p><strong class="alert">Note:</strong> Code presented here has been
modified from the original source to aid in processing and
presentation. This is <strong>NOT</strong> functional code. To review this
component's literal, functional source, please refer to its
JavaScript files as provided in <a href="http://sourceforge.net/
projects/yui/">the downloadable YUI Library distribution</a>.</p>
<h3><a name="source">Source Code</a> <span class="top">[<a href="#top">top</a>]</span></h3>
<pre class="sourceview"><span class="comment">/*
Copyright (c) 2006, Yahoo! Inc. All rights reserved.
Code licensed under the BSD License:
http://developer.yahoo.net/yui/license.txt
Version 0.11.3
*/</span>
<span class="comment">/**
* &lt;p&gt;YAHOO.widget.DateMath is used for simple date manipulation. The class is a static utility
* used for adding, subtracting, and comparing dates.&lt;/p&gt;
*/</span>
YAHOO.widget.DateMath = new <span class="reserved">function</span>() {
<span class="comment">/**
* Constant field representing Day
* <span class="attrib">@type</span> String
*/</span>
<span class="reserved">this</span>.DAY = <span class="literal">"D"</span>;
<span class="comment">/**
* Constant field representing Week
* <span class="attrib">@type</span> String
*/</span>
<span class="reserved">this</span>.WEEK = <span class="literal">"W"</span>;
<span class="comment">/**
* Constant field representing Year
* <span class="attrib">@type</span> String
*/</span>
<span class="reserved">this</span>.YEAR = <span class="literal">"Y"</span>;
<span class="comment">/**
* Constant field representing Month
* <span class="attrib">@type</span> String
*/</span>
<span class="reserved">this</span>.MONTH = <span class="literal">"M"</span>;
<span class="comment">/**
* Constant field representing one day, in milliseconds
* <span class="attrib">@type</span> Integer
*/</span>
<span class="reserved">this</span>.ONE_DAY_MS = 1000*60*60*24;
<span class="comment">/**
* Adds the specified amount of time to the this instance.
* <span class="attrib">@param</span> {Date} date The JavaScript Date object to perform addition on
* <span class="attrib">@param</span> {string} field The this field constant to be used for performing addition.
* <span class="attrib">@param</span> {Integer} amount The number of units (measured in the field constant) to add to the date.
*/</span>
<span class="reserved">this</span>.add = <span class="reserved">function</span>(date, field, amount) {
var d = new Date(date.getTime());
switch (field) {
case <span class="reserved">this</span>.MONTH:
var newMonth = date.getMonth() + amount;
var years = 0;
<span class="reserved">if</span> (newMonth &lt; 0) {
<span class="reserved">while</span> (newMonth &lt; 0) {
newMonth += 12;
years -= 1;
}
} <span class="reserved">else</span> <span class="reserved">if</span> (newMonth &gt; 11) {
<span class="reserved">while</span> (newMonth &gt; 11) {
newMonth -= 12;
years += 1;
}
}
d.setMonth(newMonth);
d.setFullYear(date.getFullYear() + years);
break;
case <span class="reserved">this</span>.DAY:
d.setDate(date.getDate() + amount);
break;
case <span class="reserved">this</span>.YEAR:
d.setFullYear(date.getFullYear() + amount);
break;
case <span class="reserved">this</span>.WEEK:
d.setDate(date.getDate() + (amount * 7));
break;
}
<span class="reserved">return</span> d;
};
<span class="comment">/**
* Subtracts the specified amount of time from the this instance.
* <span class="attrib">@param</span> {Date} date The JavaScript Date object to perform subtraction on
* <span class="attrib">@param</span> {Integer} field The this field constant to be used for performing subtraction.
* <span class="attrib">@param</span> {Integer} amount The number of units (measured in the field constant) to subtract from the date.
*/</span>
<span class="reserved">this</span>.subtract = <span class="reserved">function</span>(date, field, amount) {
<span class="reserved">return</span> <span class="reserved">this</span>.add(date, field, (amount*-1));
};
<span class="comment">/**
* Determines whether a given date is before another date on the calendar.
* <span class="attrib">@param</span> {Date} date The Date object to compare with the compare argument
* <span class="attrib">@param</span> {Date} compareTo The Date object to use for the comparison
* <span class="attrib">@return</span> {Boolean} true if the date occurs before the compared date; false if not.
*/</span>
<span class="reserved">this</span>.before = <span class="reserved">function</span>(date, compareTo) {
var ms = compareTo.getTime();
<span class="reserved">if</span> (date.getTime() &lt; ms) {
<span class="reserved">return</span> true;
} <span class="reserved">else</span> {
<span class="reserved">return</span> false;
}
};
<span class="comment">/**
* Determines whether a given date is after another date on the calendar.
* <span class="attrib">@param</span> {Date} date The Date object to compare with the compare argument
* <span class="attrib">@param</span> {Date} compareTo The Date object to use for the comparison
* <span class="attrib">@return</span> {Boolean} true if the date occurs after the compared date; false if not.
*/</span>
<span class="reserved">this</span>.after = <span class="reserved">function</span>(date, compareTo) {
var ms = compareTo.getTime();
<span class="reserved">if</span> (date.getTime() &gt; ms) {
<span class="reserved">return</span> true;
} <span class="reserved">else</span> {
<span class="reserved">return</span> false;
}
};
<span class="comment">/**
* Determines whether a given date is between two other dates on the calendar.
* <span class="attrib">@param</span> {Date} date The date to check for
* <span class="attrib">@param</span> {Date} dateBegin The start of the range
* <span class="attrib">@param</span> {Date} dateEnd The end of the range
* <span class="attrib">@return</span> {Boolean} true if the date occurs between the compared dates; false if not.
*/</span>
<span class="reserved">this</span>.between = <span class="reserved">function</span>(date, dateBegin, dateEnd) {
<span class="reserved">if</span> (<span class="reserved">this</span>.after(date, dateBegin) &amp;&amp; <span class="reserved">this</span>.before(date, dateEnd)) {
<span class="reserved">return</span> true;
} <span class="reserved">else</span> {
<span class="reserved">return</span> false;
}
};
<span class="comment">/**
* Retrieves a JavaScript Date object representing January 1 of any given year.
* <span class="attrib">@param</span> {Integer} calendarYear The calendar year for which to retrieve January 1
* <span class="attrib">@return</span> {Date} January 1 of the calendar year specified.
*/</span>
<span class="reserved">this</span>.getJan1 = <span class="reserved">function</span>(calendarYear) {
<span class="reserved">return</span> new Date(calendarYear,0,1);
};
<span class="comment">/**
* Calculates the number of days the specified date is from January 1 of the specified calendar year.
* Passing January 1 to this function would return an offset value of zero.
* <span class="attrib">@param</span> {Date} date The JavaScript date for which to find the offset
* <span class="attrib">@param</span> {Integer} calendarYear The calendar year to use for determining the offset
* <span class="attrib">@return</span> {Integer} The number of days since January 1 of the given year
*/</span>
<span class="reserved">this</span>.getDayOffset = <span class="reserved">function</span>(date, calendarYear) {
var beginYear = <span class="reserved">this</span>.getJan1(calendarYear); <span class="comment">// Find the start of the year. This will be in week 1.</span>
<span class="comment">// Find the number of days the passed in date is away from the calendar year start</span>
var dayOffset = Math.ceil((date.getTime()-beginYear.getTime()) / <span class="reserved">this</span>.ONE_DAY_MS);
<span class="reserved">return</span> dayOffset;
};
<span class="comment">/**
* Calculates the week number for the given date. This function assumes that week 1 is the
* week in which January 1 appears, regardless of whether the week consists of a full 7 days.
* The calendar year can be specified to help find what a the week number would be for a given
* date if the date overlaps years. For instance, a week may be considered week 1 of 2005, or
* week 53 of 2004. Specifying the optional calendarYear allows one to make this distinction
* easily.
* <span class="attrib">@param</span> {Date} date The JavaScript date for which to find the week number
* <span class="attrib">@param</span> {Integer} calendarYear OPTIONAL - The calendar year to use for determining the week number. Default is
* the calendar year of parameter "date".
* <span class="attrib">@param</span> {Integer} weekStartsOn OPTIONAL - The integer (0-6) representing which day a week begins on. Default is 0 (for Sunday).
* <span class="attrib">@return</span> {Integer} The week number of the given date.
*/</span>
<span class="reserved">this</span>.getWeekNumber = <span class="reserved">function</span>(date, calendarYear, weekStartsOn) {
date.setHours(12,0,0,0);
<span class="reserved">if</span> (! weekStartsOn) {
weekStartsOn = 0;
}
<span class="reserved">if</span> (! calendarYear) {
calendarYear = date.getFullYear();
}
var weekNum = -1;
var jan1 = <span class="reserved">this</span>.getJan1(calendarYear);
var jan1Offset = jan1.getDay() - weekStartsOn;
var jan1DayOfWeek = (jan1Offset &gt;= 0 ? jan1Offset : (7 + jan1Offset));
var endOfWeek1 = <span class="reserved">this</span>.add(jan1, <span class="reserved">this</span>.DAY, (6 - jan1DayOfWeek));
endOfWeek1.setHours(23,59,59,999);
var month = date.getMonth();
var day = date.getDate();
var year = date.getFullYear();
var dayOffset = <span class="reserved">this</span>.getDayOffset(date, calendarYear); <span class="comment">// Days since Jan 1, Calendar Year</span>
<span class="reserved">if</span> (dayOffset &lt; 0 || <span class="reserved">this</span>.before(date, endOfWeek1)) {
weekNum = 1;
} <span class="reserved">else</span> {
weekNum = 2;
var weekBegin = new Date(endOfWeek1.getTime() + 1);
var weekEnd = <span class="reserved">this</span>.add(weekBegin, <span class="reserved">this</span>.WEEK, 1);
<span class="reserved">while</span> (! <span class="reserved">this</span>.between(date, weekBegin, weekEnd)) {
weekBegin = <span class="reserved">this</span>.add(weekBegin, <span class="reserved">this</span>.WEEK, 1);
weekEnd = <span class="reserved">this</span>.add(weekEnd, <span class="reserved">this</span>.WEEK, 1);
weekNum += 1;
}
}
<span class="reserved">return</span> weekNum;
};
<span class="comment">/**
* Determines if a given week overlaps two different years.
* <span class="attrib">@param</span> {Date} weekBeginDate The JavaScript Date representing the first day of the week.
* <span class="attrib">@return</span> {Boolean} true if the date overlaps two different years.
*/</span>
<span class="reserved">this</span>.isYearOverlapWeek = <span class="reserved">function</span>(weekBeginDate) {
var overlaps = false;
var nextWeek = <span class="reserved">this</span>.add(weekBeginDate, <span class="reserved">this</span>.DAY, 6);
<span class="reserved">if</span> (nextWeek.getFullYear() != weekBeginDate.getFullYear()) {
overlaps = true;
}
<span class="reserved">return</span> overlaps;
};
<span class="comment">/**
* Determines if a given week overlaps two different months.
* <span class="attrib">@param</span> {Date} weekBeginDate The JavaScript Date representing the first day of the week.
* <span class="attrib">@return</span> {Boolean} true if the date overlaps two different months.
*/</span>
<span class="reserved">this</span>.isMonthOverlapWeek = <span class="reserved">function</span>(weekBeginDate) {
var overlaps = false;
var nextWeek = <span class="reserved">this</span>.add(weekBeginDate, <span class="reserved">this</span>.DAY, 6);
<span class="reserved">if</span> (nextWeek.getMonth() != weekBeginDate.getMonth()) {
overlaps = true;
}
<span class="reserved">return</span> overlaps;
};
<span class="comment">/**
* Gets the first day of a month containing a given date.
* <span class="attrib">@param</span> {Date} date The JavaScript Date used to calculate the month start
* <span class="attrib">@return</span> {Date} The JavaScript Date representing the first day of the month
*/</span>
<span class="reserved">this</span>.findMonthStart = <span class="reserved">function</span>(date) {
var start = new Date(date.getFullYear(), date.getMonth(), 1);
<span class="reserved">return</span> start;
};
<span class="comment">/**
* Gets the last day of a month containing a given date.
* <span class="attrib">@param</span> {Date} date The JavaScript Date used to calculate the month end
* <span class="attrib">@return</span> {Date} The JavaScript Date representing the last day of the month
*/</span>
<span class="reserved">this</span>.findMonthEnd = <span class="reserved">function</span>(date) {
var start = <span class="reserved">this</span>.findMonthStart(date);
var nextMonth = <span class="reserved">this</span>.add(start, <span class="reserved">this</span>.MONTH, 1);
var end = <span class="reserved">this</span>.subtract(nextMonth, <span class="reserved">this</span>.DAY, 1);
<span class="reserved">return</span> end;
};
<span class="comment">/**
* Clears the time fields from a given date, effectively setting the time to midnight.
* <span class="attrib">@param</span> {Date} date The JavaScript Date for which the time fields will be cleared
* <span class="attrib">@return</span> {Date} The JavaScript Date cleared of all time fields
*/</span>
<span class="reserved">this</span>.clearTime = <span class="reserved">function</span>(date) {
date.setHours(0,0,0,0);
<span class="reserved">return</span> date;
};
}</pre>
</div>
</div>
</div>
<div id="footer">
<hr />
Copyright &copy; 2006 Yahoo! Inc. All rights reserved.
<br /><br />
Documentation generated by <a href="http://jsdoc.sourceforge.net/">
JSDoc</a>
</div>
</body>
</html>