2 Copyright (c) 2011, Yahoo! Inc. All rights reserved.
3 Code licensed under the BSD License:
4 http://developer.yahoo.com/yui/license.html
8 * The datemath module provides utility methods for basic JavaScript Date object manipulation and
15 * YAHOO.widget.DateMath is used for simple date manipulation. The class is a static utility
16 * used for adding, subtracting, and comparing dates.
17 * @namespace YAHOO.widget
20 YAHOO.widget.DateMath = {
22 * Constant field representing Day
31 * Constant field representing Week
40 * Constant field representing Year
49 * Constant field representing Month
58 * Constant field representing one day, in milliseconds
59 * @property ONE_DAY_MS
64 ONE_DAY_MS : 1000*60*60*24,
67 * Constant field representing the date in first week of January
68 * which identifies the first week of the year.
70 * In the U.S, Jan 1st is normally used based on a Sunday start of week.
71 * ISO 8601, used widely throughout Europe, uses Jan 4th, based on a Monday start of week.
73 * @property WEEK_ONE_JAN_DATE
77 WEEK_ONE_JAN_DATE : 1,
80 * Adds the specified amount of time to the this instance.
82 * @param {Date} date The JavaScript Date object to perform addition on
83 * @param {String} field The field constant to be used for performing addition.
84 * @param {Number} amount The number of units (measured in the field constant) to add to the date.
85 * @return {Date} The resulting Date object
87 add : function(date, field, amount) {
88 var d = new Date(date.getTime());
91 var newMonth = date.getMonth() + amount;
95 while (newMonth < 0) {
99 } else if (newMonth > 11) {
100 while (newMonth > 11) {
106 d.setMonth(newMonth);
107 d.setFullYear(date.getFullYear() + years);
110 this._addDays(d, amount);
111 // d.setDate(date.getDate() + amount);
114 d.setFullYear(date.getFullYear() + amount);
117 this._addDays(d, (amount * 7));
118 // d.setDate(date.getDate() + (amount * 7));
125 * Private helper method to account for bug in Safari 2 (webkit < 420)
126 * when Date.setDate(n) is called with n less than -128 or greater than 127.
128 * Fix approach and original findings are available here:
129 * http://brianary.blogspot.com/2006/03/safari-date-bug.html
132 * @param {Date} d JavaScript date object
133 * @param {Number} nDays The number of days to add to the date object (can be negative)
136 _addDays : function(d, nDays) {
137 if (YAHOO.env.ua.webkit && YAHOO.env.ua.webkit < 420) {
139 // Ensure we don't go below -128 (getDate() is always 1 to 31, so we won't go above 127)
140 for(var min = -128; nDays < min; nDays -= min) {
141 d.setDate(d.getDate() + min);
144 // Ensure we don't go above 96 + 31 = 127
145 for(var max = 96; nDays > max; nDays -= max) {
146 d.setDate(d.getDate() + max);
149 // nDays should be remainder between -128 and 96
151 d.setDate(d.getDate() + nDays);
155 * Subtracts the specified amount of time from the this instance.
157 * @param {Date} date The JavaScript Date object to perform subtraction on
158 * @param {Number} field The this field constant to be used for performing subtraction.
159 * @param {Number} amount The number of units (measured in the field constant) to subtract from the date.
160 * @return {Date} The resulting Date object
162 subtract : function(date, field, amount) {
163 return this.add(date, field, (amount*-1));
167 * Determines whether a given date is before another date on the calendar.
169 * @param {Date} date The Date object to compare with the compare argument
170 * @param {Date} compareTo The Date object to use for the comparison
171 * @return {Boolean} true if the date occurs before the compared date; false if not.
173 before : function(date, compareTo) {
174 var ms = compareTo.getTime();
175 if (date.getTime() < ms) {
183 * Determines whether a given date is after another date on the calendar.
185 * @param {Date} date The Date object to compare with the compare argument
186 * @param {Date} compareTo The Date object to use for the comparison
187 * @return {Boolean} true if the date occurs after the compared date; false if not.
189 after : function(date, compareTo) {
190 var ms = compareTo.getTime();
191 if (date.getTime() > ms) {
199 * Determines whether a given date is between two other dates on the calendar.
201 * @param {Date} date The date to check for
202 * @param {Date} dateBegin The start of the range
203 * @param {Date} dateEnd The end of the range
204 * @return {Boolean} true if the date occurs between the compared dates; false if not.
206 between : function(date, dateBegin, dateEnd) {
207 if (this.after(date, dateBegin) && this.before(date, dateEnd)) {
215 * Retrieves a JavaScript Date object representing January 1 of any given year.
217 * @param {Number} calendarYear The calendar year for which to retrieve January 1
218 * @return {Date} January 1 of the calendar year specified.
220 getJan1 : function(calendarYear) {
221 return this.getDate(calendarYear,0,1);
225 * Calculates the number of days the specified date is from January 1 of the specified calendar year.
226 * Passing January 1 to this function would return an offset value of zero.
227 * @method getDayOffset
228 * @param {Date} date The JavaScript date for which to find the offset
229 * @param {Number} calendarYear The calendar year to use for determining the offset
230 * @return {Number} The number of days since January 1 of the given year
232 getDayOffset : function(date, calendarYear) {
233 var beginYear = this.getJan1(calendarYear); // Find the start of the year. This will be in week 1.
235 // Find the number of days the passed in date is away from the calendar year start
236 var dayOffset = Math.ceil((date.getTime()-beginYear.getTime()) / this.ONE_DAY_MS);
241 * Calculates the week number for the given date. Can currently support standard
242 * U.S. week numbers, based on Jan 1st defining the 1st week of the year, and
243 * ISO8601 week numbers, based on Jan 4th defining the 1st week of the year.
245 * @method getWeekNumber
246 * @param {Date} date The JavaScript date for which to find the week number
247 * @param {Number} firstDayOfWeek The index of the first day of the week (0 = Sun, 1 = Mon ... 6 = Sat).
249 * @param {Number} janDate The date in the first week of January which defines week one for the year
250 * Defaults to the value of YAHOO.widget.DateMath.WEEK_ONE_JAN_DATE, which is 1 (Jan 1st).
251 * For the U.S, this is normally Jan 1st. ISO8601 uses Jan 4th to define the first week of the year.
253 * @return {Number} The number of the week containing the given date.
255 getWeekNumber : function(date, firstDayOfWeek, janDate) {
258 firstDayOfWeek = firstDayOfWeek || 0;
259 janDate = janDate || this.WEEK_ONE_JAN_DATE;
261 var targetDate = this.clearTime(date),
265 if (targetDate.getDay() === firstDayOfWeek) {
266 startOfWeek = targetDate;
268 startOfWeek = this.getFirstDayOfWeek(targetDate, firstDayOfWeek);
271 var startYear = startOfWeek.getFullYear();
273 // DST shouldn't be a problem here, math is quicker than setDate();
274 endOfWeek = new Date(startOfWeek.getTime() + 6*this.ONE_DAY_MS);
277 if (startYear !== endOfWeek.getFullYear() && endOfWeek.getDate() >= janDate) {
278 // If years don't match, endOfWeek is in Jan. and if the
279 // week has WEEK_ONE_JAN_DATE in it, it's week one by definition.
282 // Get the 1st day of the 1st week, and
283 // find how many days away we are from it.
284 var weekOne = this.clearTime(this.getDate(startYear, 0, janDate)),
285 weekOneDayOne = this.getFirstDayOfWeek(weekOne, firstDayOfWeek);
287 // Round days to smoothen out 1 hr DST diff
288 var daysDiff = Math.round((targetDate.getTime() - weekOneDayOne.getTime())/this.ONE_DAY_MS);
291 var rem = daysDiff % 7;
292 var weeksDiff = (daysDiff - rem)/7;
293 weekNum = weeksDiff + 1;
299 * Get the first day of the week, for the give date.
300 * @param {Date} dt The date in the week for which the first day is required.
301 * @param {Number} startOfWeek The index for the first day of the week, 0 = Sun, 1 = Mon ... 6 = Sat (defaults to 0)
302 * @return {Date} The first day of the week
304 getFirstDayOfWeek : function (dt, startOfWeek) {
305 startOfWeek = startOfWeek || 0;
306 var dayOfWeekIndex = dt.getDay(),
307 dayOfWeek = (dayOfWeekIndex - startOfWeek + 7) % 7;
309 return this.subtract(dt, this.DAY, dayOfWeek);
313 * Determines if a given week overlaps two different years.
314 * @method isYearOverlapWeek
315 * @param {Date} weekBeginDate The JavaScript Date representing the first day of the week.
316 * @return {Boolean} true if the date overlaps two different years.
318 isYearOverlapWeek : function(weekBeginDate) {
319 var overlaps = false;
320 var nextWeek = this.add(weekBeginDate, this.DAY, 6);
321 if (nextWeek.getFullYear() != weekBeginDate.getFullYear()) {
328 * Determines if a given week overlaps two different months.
329 * @method isMonthOverlapWeek
330 * @param {Date} weekBeginDate The JavaScript Date representing the first day of the week.
331 * @return {Boolean} true if the date overlaps two different months.
333 isMonthOverlapWeek : function(weekBeginDate) {
334 var overlaps = false;
335 var nextWeek = this.add(weekBeginDate, this.DAY, 6);
336 if (nextWeek.getMonth() != weekBeginDate.getMonth()) {
343 * Gets the first day of a month containing a given date.
344 * @method findMonthStart
345 * @param {Date} date The JavaScript Date used to calculate the month start
346 * @return {Date} The JavaScript Date representing the first day of the month
348 findMonthStart : function(date) {
349 var start = this.getDate(date.getFullYear(), date.getMonth(), 1);
354 * Gets the last day of a month containing a given date.
355 * @method findMonthEnd
356 * @param {Date} date The JavaScript Date used to calculate the month end
357 * @return {Date} The JavaScript Date representing the last day of the month
359 findMonthEnd : function(date) {
360 var start = this.findMonthStart(date);
361 var nextMonth = this.add(start, this.MONTH, 1);
362 var end = this.subtract(nextMonth, this.DAY, 1);
367 * Clears the time fields from a given date, effectively setting the time to 12 noon.
369 * @param {Date} date The JavaScript Date for which the time fields will be cleared
370 * @return {Date} The JavaScript Date cleared of all time fields
372 clearTime : function(date) {
373 date.setHours(12,0,0,0);
378 * Returns a new JavaScript Date object, representing the given year, month and date. Time fields (hr, min, sec, ms) on the new Date object
379 * are set to 0. The method allows Date instances to be created with the a year less than 100. "new Date(year, month, date)" implementations
380 * set the year to 19xx if a year (xx) which is less than 100 is provided.
382 * <em>NOTE:</em>Validation on argument values is not performed. It is the caller's responsibility to ensure
383 * arguments are valid as per the ECMAScript-262 Date object specification for the new Date(year, month[, date]) constructor.
386 * @param {Number} y Year.
387 * @param {Number} m Month index from 0 (Jan) to 11 (Dec).
388 * @param {Number} d (optional) Date from 1 to 31. If not provided, defaults to 1.
389 * @return {Date} The JavaScript date object with year, month, date set as provided.
391 getDate : function(y, m, d) {
393 if (YAHOO.lang.isUndefined(d)) {
397 dt = new Date(y, m, d);
403 dt.setHours(0,0,0,0);
408 YAHOO.register("datemath", YAHOO.widget.DateMath, {version: "2.9.0", build: "2800"});