3
Copyright 2012 Yahoo! Inc. All rights reserved.
4
Licensed under the BSD License.
5
http://yuilibrary.com/license/
7
YUI.add('datatype-date-math', function(Y) {
10
* Datatype Date Math submodule.
13
* @submodule datatype-date-math
18
Y.mix(Y.namespace("DataType.Date"), {
21
* Checks whether a native JavaScript Date contains a valid value.
24
* @param oDate {Date} Date in the month for which the number of days is desired.
25
* @return {Boolean} True if the date argument contains a valid value.
27
isValidDate : function (oDate) {
28
if(LANG.isDate(oDate) && (isFinite(oDate)) && (oDate != "Invalid Date") && !isNaN(oDate) && (oDate != null)) {
37
* Checks whether two dates correspond to the same date and time.
40
* @param aDate {Date} The first date to compare.
41
* @param bDate {Date} The second date to compare.
42
* @return {Boolean} True if the two dates correspond to the same
45
areEqual : function (aDate, bDate) {
46
return (this.isValidDate(aDate) && this.isValidDate(bDate) && (aDate.getTime() == bDate.getTime()));
50
* Checks whether the first date comes later than the second.
53
* @param aDate {Date} The first date to compare.
54
* @param bDate {Date} The second date to compare.
55
* @return {Boolean} True if the first date is later than the second.
57
isGreater : function (aDate, bDate) {
58
return (this.isValidDate(aDate) && this.isValidDate(bDate) && (aDate.getTime() > bDate.getTime()));
62
* Checks whether the first date comes later than or is the same as
65
* @method isGreaterOrEqual
66
* @param aDate {Date} The first date to compare.
67
* @param bDate {Date} The second date to compare.
68
* @return {Boolean} True if the first date is later than or
69
* the same as the second.
71
isGreaterOrEqual : function (aDate, bDate) {
72
return (this.isValidDate(aDate) && this.isValidDate(bDate) && (aDate.getTime() >= bDate.getTime()));
77
* Checks whether the date is between two other given dates.
80
* @param aDate {Date} The date to check
81
* @param bDate {Date} Lower bound of the range.
82
* @param cDate {Date} Higher bound of the range.
83
* @return {Boolean} True if the date is between the two other given dates.
85
isInRange : function (aDate, bDate, cDate) {
86
return (this.isGreaterOrEqual(aDate, bDate) && this.isGreaterOrEqual(cDate, aDate));
90
* Adds a specified number of days to the given date.
93
* @param oDate {Date} The date to add days to.
94
* @param numMonths {Number} The number of days to add (can be negative)
95
* @return {Date} A new Date with the specified number of days
96
* added to the original date.
98
addDays : function (oDate, numDays) {
99
return new Date(oDate.getTime() + 86400000*numDays);
104
* Adds a specified number of months to the given date.
107
* @param oDate {Date} The date to add months to.
108
* @param numMonths {Number} The number of months to add (can be negative)
109
* @return {Date} A new Date with the specified number of months
110
* added to the original date.
112
addMonths : function (oDate, numMonths) {
113
var newYear = oDate.getFullYear();
114
var newMonth = oDate.getMonth() + numMonths;
116
newYear = Math.floor(newYear + newMonth / 12);
117
newMonth = (newMonth % 12 + 12) % 12;
119
var newDate = new Date (oDate.getTime());
120
newDate.setYear(newYear);
121
newDate.setMonth(newMonth);
127
* Adds a specified number of years to the given date.
130
* @param oDate {Date} The date to add years to.
131
* @param numYears {Number} The number of years to add (can be negative)
132
* @return {Date} A new Date with the specified number of years
133
* added to the original date.
135
addYears : function (oDate, numYears) {
136
var newYear = oDate.getFullYear() + numYears;
137
var newDate = new Date(oDate.getTime());
139
newDate.setYear(newYear);
144
* Lists all dates in a given month.
146
* @method listOfDatesInMonth
147
* @param oDate {Date} The date corresponding to the month for
148
* which a list of dates is required.
149
* @return {Array} An `Array` of `Date`s from a given month.
151
listOfDatesInMonth : function (oDate) {
152
if (!this.isValidDate(oDate)) {
156
var daysInMonth = this.daysInMonth(oDate),
157
year = oDate.getFullYear(),
158
month = oDate.getMonth(),
161
for (var day = 1; day <= daysInMonth; day++) {
162
output.push(new Date(year, month, day, 12, 0, 0));
169
* Takes a native JavaScript Date and returns the number of days
170
* in the month that the given date belongs to.
172
* @method daysInMonth
173
* @param oDate {Date} Date in the month for which the number
174
* of days is desired.
175
* @return {Number} A number (either 28, 29, 30 or 31) of days
176
* in the given month.
178
daysInMonth : function (oDate) {
179
if (!this.isValidDate(oDate)) {
183
var mon = oDate.getMonth();
184
var lengths = [31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31];
191
var year = oDate.getFullYear();
192
if (year%400 === 0) {
195
else if (year%100 === 0) {
198
else if (year%4 === 0) {
210
}, '3.5.0' ,{requires:['yui-base']});