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)) {
33 Y.log("Could not validate data as type Date", "warn", "date");
39 * Checks whether two dates correspond to the same date and time.
42 * @param aDate {Date} The first date to compare.
43 * @param bDate {Date} The second date to compare.
44 * @return {Boolean} True if the two dates correspond to the same
47 areEqual : function (aDate, bDate) {
48 return (this.isValidDate(aDate) && this.isValidDate(bDate) && (aDate.getTime() == bDate.getTime()));
52 * Checks whether the first date comes later than the second.
55 * @param aDate {Date} The first date to compare.
56 * @param bDate {Date} The second date to compare.
57 * @return {Boolean} True if the first date is later than the second.
59 isGreater : function (aDate, bDate) {
60 return (this.isValidDate(aDate) && this.isValidDate(bDate) && (aDate.getTime() > bDate.getTime()));
64 * Checks whether the first date comes later than or is the same as
67 * @method isGreaterOrEqual
68 * @param aDate {Date} The first date to compare.
69 * @param bDate {Date} The second date to compare.
70 * @return {Boolean} True if the first date is later than or
71 * the same as the second.
73 isGreaterOrEqual : function (aDate, bDate) {
74 return (this.isValidDate(aDate) && this.isValidDate(bDate) && (aDate.getTime() >= bDate.getTime()));
79 * Checks whether the date is between two other given dates.
82 * @param aDate {Date} The date to check
83 * @param bDate {Date} Lower bound of the range.
84 * @param cDate {Date} Higher bound of the range.
85 * @return {Boolean} True if the date is between the two other given dates.
87 isInRange : function (aDate, bDate, cDate) {
88 return (this.isGreaterOrEqual(aDate, bDate) && this.isGreaterOrEqual(cDate, aDate));
92 * Adds a specified number of days to the given date.
95 * @param oDate {Date} The date to add days to.
96 * @param numDays {Number} The number of days to add (can be negative)
97 * @return {Date} A new Date with the specified number of days
98 * added to the original date.
100 addDays : function (oDate, numDays) {
101 return new Date(oDate.getTime() + 86400000*numDays);
106 * Adds a specified number of months to the given date.
109 * @param oDate {Date} The date to add months to.
110 * @param numMonths {Number} The number of months to add (can be negative)
111 * @return {Date} A new Date with the specified number of months
112 * added to the original date.
114 addMonths : function (oDate, numMonths) {
115 var newYear = oDate.getFullYear();
116 var newMonth = oDate.getMonth() + numMonths;
118 newYear = Math.floor(newYear + newMonth / 12);
119 newMonth = (newMonth % 12 + 12) % 12;
121 var newDate = new Date (oDate.getTime());
122 newDate.setFullYear(newYear);
123 newDate.setMonth(newMonth);
129 * Adds a specified number of years to the given date.
132 * @param oDate {Date} The date to add years to.
133 * @param numYears {Number} The number of years to add (can be negative)
134 * @return {Date} A new Date with the specified number of years
135 * added to the original date.
137 addYears : function (oDate, numYears) {
138 var newYear = oDate.getFullYear() + numYears;
139 var newDate = new Date(oDate.getTime());
141 newDate.setFullYear(newYear);
146 * Lists all dates in a given month.
148 * @method listOfDatesInMonth
149 * @param oDate {Date} The date corresponding to the month for
150 * which a list of dates is required.
151 * @return {Array} An `Array` of `Date`s from a given month.
153 listOfDatesInMonth : function (oDate) {
154 if (!this.isValidDate(oDate)) {
158 var daysInMonth = this.daysInMonth(oDate),
159 year = oDate.getFullYear(),
160 month = oDate.getMonth(),
163 for (var day = 1; day <= daysInMonth; day++) {
164 output.push(new Date(year, month, day, 12, 0, 0));
171 * Takes a native JavaScript Date and returns the number of days
172 * in the month that the given date belongs to.
174 * @method daysInMonth
175 * @param oDate {Date} Date in the month for which the number
176 * of days is desired.
177 * @return {Number} A number (either 28, 29, 30 or 31) of days
178 * in the given month.
180 daysInMonth : function (oDate) {
181 if (!this.isValidDate(oDate)) {
185 var mon = oDate.getMonth();
186 var lengths = [31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31];
193 var year = oDate.getFullYear();
194 if (year%400 === 0) {
197 else if (year%100 === 0) {
200 else if (year%4 === 0) {
211 Y.namespace("DataType");
212 Y.DataType.Date = Y.Date;
215 }, '3.13.0', {"requires": ["yui-base"]});