datatype-date-math.js revision 829f44d633f4910c12181f3295e5c6b996d7e559
524N/AYUI.add('datatype-date-math', function(Y) {
524N/A
524N/A/**
524N/A * Datatype Date Math submodule.
524N/A *
524N/A * @module datatype
524N/A * @submodule datatype-date-math
524N/A * @for DataType.Date
524N/A */
524N/Avar LANG = Y.Lang;
524N/A
524N/AY.mix(Y.namespace("DataType.Date"), {
524N/A
524N/A /**
524N/A * Checks whether a native JavaScript Date contains a valid value.
524N/A * @for DataType.Date
524N/A * @method isValidDate
524N/A * @param oDate {Date} Date in the month for which the number of days is desired.
524N/A * @return {Boolean} True if the date argument contains a valid value.
524N/A */
524N/A isValidDate : function (oDate) {
524N/A if(LANG.isDate(oDate) && (isFinite(oDate)) && (oDate != "Invalid Date") && !isNaN(oDate) && (oDate != null)) {
524N/A return true;
524N/A }
524N/A else {
524N/A return false;
524N/A }
524N/A },
524N/A
524N/A /**
524N/A * Checks whether two dates correspond to the same date and time.
524N/A * @for DataType.Date
524N/A * @method areEqual
524N/A * @param aDate {Date} The first date to compare.
524N/A * @param bDate {Date} The second date to compare.
524N/A * @return {Boolean} True if the two dates correspond to the same
524N/A * date and time.
524N/A */
524N/A areEqual : function (aDate, bDate) {
524N/A return (this.isValidDate(aDate) && this.isValidDate(bDate) && (aDate.getTime() == bDate.getTime()));
524N/A },
524N/A
524N/A /**
524N/A * Checks whether the first date comes later than the second.
524N/A * @for DataType.Date
524N/A * @method isGreater
524N/A * @param aDate {Date} The first date to compare.
524N/A * @param bDate {Date} The second date to compare.
524N/A * @return {Boolean} True if the first date is later than the second.
524N/A */
524N/A isGreater : function (aDate, bDate) {
524N/A return (this.isValidDate(aDate) && this.isValidDate(bDate) && (aDate.getTime() > bDate.getTime()));
524N/A },
524N/A
524N/A /**
524N/A * Checks whether the first date comes later than or is the same as
524N/A * the second.
524N/A * @for DataType.Date
524N/A * @method isGreaterOrEqual
524N/A * @param aDate {Date} The first date to compare.
524N/A * @param bDate {Date} The second date to compare.
524N/A * @return {Boolean} True if the first date is later than or
524N/A * the same as the second.
524N/A */
524N/A isGreaterOrEqual : function (aDate, bDate) {
524N/A return (this.isValidDate(aDate) && this.isValidDate(bDate) && (aDate.getTime() >= bDate.getTime()));
524N/A },
524N/A
524N/A
524N/A /**
524N/A * Checks whether the date is between two other given dates.
524N/A * @for DataType.Date
524N/A * @method isInRange
524N/A * @param aDate {Date} The date to check
524N/A * @param bDate {Date} Lower bound of the range.
524N/A * @param cDate {Date} Higher bound of the range.
524N/A * @return {Boolean} True if the date is between the two other given dates.
524N/A */
524N/A isInRange : function (aDate, bDate, cDate) {
524N/A return (this.isGreaterOrEqual(aDate, bDate) && this.isGreaterOrEqual(cDate, aDate));
524N/A },
524N/A
524N/A /**
524N/A * Adds a specified number of days to the given date.
524N/A * @for DataType.Date
524N/A * @method addDays
524N/A * @param oDate {Date} The date to add days to.
524N/A * @param numMonths {Number} The number of days to add (can be negative)
524N/A * @return {Date} A new Date with the specified number of days
524N/A * added to the original date.
524N/A */
524N/A addDays : function (oDate, numDays) {
524N/A return new Date(oDate.getTime() + 86400000*numDays);
524N/A },
524N/A
524N/A
524N/A /**
524N/A * Adds a specified number of months to the given date.
524N/A * @for DataType.Date
524N/A * @method addMonths
524N/A * @param oDate {Date} The date to add months to.
524N/A * @param numMonths {Number} The number of months to add (can be negative)
524N/A * @return {Date} A new Date with the specified number of months
524N/A * added to the original date.
524N/A */
524N/A addMonths : function (oDate, numMonths) {
524N/A var newYear = oDate.getFullYear();
524N/A var newMonth = oDate.getMonth() + numMonths;
524N/A
524N/A newYear = Math.floor(newYear + newMonth / 12);
524N/A newMonth = (newMonth % 12 + 12) % 12;
524N/A
524N/A var newDate = new Date (oDate.getTime());
524N/A newDate.setYear(newYear);
524N/A newDate.setMonth(newMonth);
524N/A
524N/A return newDate;
524N/A },
524N/A
524N/A /**
524N/A * Adds a specified number of years to the given date.
524N/A * @for DataType.Date
524N/A * @method addYears
524N/A * @param oDate {Date} The date to add years to.
524N/A * @param numYears {Number} The number of years to add (can be negative)
524N/A * @return {Date} A new Date with the specified number of years
524N/A * added to the original date.
524N/A */
524N/A addYears : function (oDate, numYears) {
524N/A var newYear = oDate.getFullYear() + numYears;
524N/A var newDate = new Date(oDate.getTime());
524N/A
524N/A newDate.setYear(newYear);
524N/A return newDate;
524N/A },
524N/A
524N/A /**
524N/A * Lists all dates in a given month.
524N/A * @for DataType.Date
524N/A * @method listOfDatesInMonth
524N/A * @param oDate {Date} The date corresponding to the month for
524N/A * which a list of dates is required.
524N/A * @return {Array} An `Array` of `Date`s from a given month.
524N/A */
524N/A listOfDatesInMonth : function (oDate) {
524N/A if (!this.isValidDate(oDate)) {
return [];
}
var daysInMonth = this.daysInMonth(oDate),
year = oDate.getFullYear(),
month = oDate.getMonth(),
output = [];
for (var day = 1; day <= daysInMonth; day++) {
output.push(new Date(year, month, day, 12, 0, 0));
}
return output;
},
/**
* Takes a native JavaScript Date and returns the number of days
* in the month that the given date belongs to.
* @for DataType.Date
* @method daysInMonth
* @param oDate {Date} Date in the month for which the number
* of days is desired.
* @return {Number} A number (either 28, 29, 30 or 31) of days
* in the given month.
*/
daysInMonth : function (oDate) {
if (!this.isValidDate(oDate)) {
return 0;
}
var mon = oDate.getMonth();
var lengths = [31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31];
if (mon != 1) {
return lengths[mon];
}
else {
var year = oDate.getFullYear();
if (year%400 === 0) {
return 29;
}
else if (year%100 === 0) {
return 28;
}
else if (year%4 === 0) {
return 29;
}
else {
return 28;
}
}
}
});
}, '@VERSION@' ,{requires:['yui-base']});