2 YUI 3.13.0 (build 508226d)
3 Copyright 2013 Yahoo! Inc. All rights reserved.
4 Licensed under the BSD License.
5 http://yuilibrary.com/license/
8 YUI.add('datatype-date-math', function (Y, NAME) {
11 * Date Math submodule.
13 * @module datatype-date
14 * @submodule datatype-date-math
19 Y.mix(Y.namespace("Date"), {
22 * Checks whether a native JavaScript Date contains a valid value.
25 * @param oDate {Date} Date in the month for which the number of days is desired.
26 * @return {Boolean} True if the date argument contains a valid value.
28 isValidDate : function (oDate) {
29 if(LANG.isDate(oDate) && (isFinite(oDate)) && (oDate != "Invalid Date") && !isNaN(oDate) && (oDate != null)) {
38 * Checks whether two dates correspond to the same date and time.
41 * @param aDate {Date} The first date to compare.
42 * @param bDate {Date} The second date to compare.
43 * @return {Boolean} True if the two dates correspond to the same
46 areEqual : function (aDate, bDate) {
47 return (this.isValidDate(aDate) && this.isValidDate(bDate) && (aDate.getTime() == bDate.getTime()));
51 * Checks whether the first date comes later than the second.
54 * @param aDate {Date} The first date to compare.
55 * @param bDate {Date} The second date to compare.
56 * @return {Boolean} True if the first date is later than the second.
58 isGreater : function (aDate, bDate) {
59 return (this.isValidDate(aDate) && this.isValidDate(bDate) && (aDate.getTime() > bDate.getTime()));
63 * Checks whether the first date comes later than or is the same as
66 * @method isGreaterOrEqual
67 * @param aDate {Date} The first date to compare.
68 * @param bDate {Date} The second date to compare.
69 * @return {Boolean} True if the first date is later than or
70 * the same as the second.
72 isGreaterOrEqual : function (aDate, bDate) {
73 return (this.isValidDate(aDate) && this.isValidDate(bDate) && (aDate.getTime() >= bDate.getTime()));
78 * Checks whether the date is between two other given dates.
81 * @param aDate {Date} The date to check
82 * @param bDate {Date} Lower bound of the range.
83 * @param cDate {Date} Higher bound of the range.
84 * @return {Boolean} True if the date is between the two other given dates.
86 isInRange : function (aDate, bDate, cDate) {
87 return (this.isGreaterOrEqual(aDate, bDate) && this.isGreaterOrEqual(cDate, aDate));
91 * Adds a specified number of days to the given date.
94 * @param oDate {Date} The date to add days to.
95 * @param numDays {Number} The number of days to add (can be negative)
96 * @return {Date} A new Date with the specified number of days
97 * added to the original date.
99 addDays : function (oDate, numDays) {
100 return new Date(oDate.getTime() + 86400000*numDays);
105 * Adds a specified number of months to the given date.
108 * @param oDate {Date} The date to add months to.
109 * @param numMonths {Number} The number of months to add (can be negative)
110 * @return {Date} A new Date with the specified number of months
111 * added to the original date.
113 addMonths : function (oDate, numMonths) {
114 var newYear = oDate.getFullYear();
115 var newMonth = oDate.getMonth() + numMonths;
117 newYear = Math.floor(newYear + newMonth / 12);
118 newMonth = (newMonth % 12 + 12) % 12;
120 var newDate = new Date (oDate.getTime());
121 newDate.setFullYear(newYear);
122 newDate.setMonth(newMonth);
128 * Adds a specified number of years to the given date.
131 * @param oDate {Date} The date to add years to.
132 * @param numYears {Number} The number of years to add (can be negative)
133 * @return {Date} A new Date with the specified number of years
134 * added to the original date.
136 addYears : function (oDate, numYears) {
137 var newYear = oDate.getFullYear() + numYears;
138 var newDate = new Date(oDate.getTime());
140 newDate.setFullYear(newYear);
145 * Lists all dates in a given month.
147 * @method listOfDatesInMonth
148 * @param oDate {Date} The date corresponding to the month for
149 * which a list of dates is required.
150 * @return {Array} An `Array` of `Date`s from a given month.
152 listOfDatesInMonth : function (oDate) {
153 if (!this.isValidDate(oDate)) {
157 var daysInMonth = this.daysInMonth(oDate),
158 year = oDate.getFullYear(),
159 month = oDate.getMonth(),
162 for (var day = 1; day <= daysInMonth; day++) {
163 output.push(new Date(year, month, day, 12, 0, 0));
170 * Takes a native JavaScript Date and returns the number of days
171 * in the month that the given date belongs to.
173 * @method daysInMonth
174 * @param oDate {Date} Date in the month for which the number
175 * of days is desired.
176 * @return {Number} A number (either 28, 29, 30 or 31) of days
177 * in the given month.
179 daysInMonth : function (oDate) {
180 if (!this.isValidDate(oDate)) {
184 var mon = oDate.getMonth();
185 var lengths = [31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31];
192 var year = oDate.getFullYear();
193 if (year%400 === 0) {
196 else if (year%100 === 0) {
199 else if (year%4 === 0) {
210 Y.namespace("DataType");
211 Y.DataType.Date = Y.Date;
214 }, '3.13.0', {"requires": ["yui-base"]});