Show:
  1. /**
  2. * Date Math submodule.
  3. *
  4. * @module datatype-date
  5. * @submodule datatype-date-math
  6. * @for Date
  7. */
  8. var LANG = Y.Lang;
  9. Y.mix(Y.namespace("Date"), {
  10. /**
  11. * Checks whether a native JavaScript Date contains a valid value.
  12. * @for Date
  13. * @method isValidDate
  14. * @param oDate {Date} Date in the month for which the number of days is desired.
  15. * @return {Boolean} True if the date argument contains a valid value.
  16. */
  17. isValidDate : function (oDate) {
  18. if(LANG.isDate(oDate) && (isFinite(oDate)) && (oDate != "Invalid Date") && !isNaN(oDate) && (oDate != null)) {
  19. return true;
  20. }
  21. else {
  22. Y.log("Could not validate data as type Date", "warn", "date");
  23. return false;
  24. }
  25. },
  26. /**
  27. * Checks whether two dates correspond to the same date and time.
  28. * @for Date
  29. * @method areEqual
  30. * @param aDate {Date} The first date to compare.
  31. * @param bDate {Date} The second date to compare.
  32. * @return {Boolean} True if the two dates correspond to the same
  33. * date and time.
  34. */
  35. areEqual : function (aDate, bDate) {
  36. return (this.isValidDate(aDate) && this.isValidDate(bDate) && (aDate.getTime() == bDate.getTime()));
  37. },
  38. /**
  39. * Checks whether the first date comes later than the second.
  40. * @for Date
  41. * @method isGreater
  42. * @param aDate {Date} The first date to compare.
  43. * @param bDate {Date} The second date to compare.
  44. * @return {Boolean} True if the first date is later than the second.
  45. */
  46. isGreater : function (aDate, bDate) {
  47. return (this.isValidDate(aDate) && this.isValidDate(bDate) && (aDate.getTime() > bDate.getTime()));
  48. },
  49. /**
  50. * Checks whether the first date comes later than or is the same as
  51. * the second.
  52. * @for Date
  53. * @method isGreaterOrEqual
  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 or
  57. * the same as the second.
  58. */
  59. isGreaterOrEqual : function (aDate, bDate) {
  60. return (this.isValidDate(aDate) && this.isValidDate(bDate) && (aDate.getTime() >= bDate.getTime()));
  61. },
  62. /**
  63. * Checks whether the date is between two other given dates.
  64. * @for Date
  65. * @method isInRange
  66. * @param aDate {Date} The date to check
  67. * @param bDate {Date} Lower bound of the range.
  68. * @param cDate {Date} Higher bound of the range.
  69. * @return {Boolean} True if the date is between the two other given dates.
  70. */
  71. isInRange : function (aDate, bDate, cDate) {
  72. return (this.isGreaterOrEqual(aDate, bDate) && this.isGreaterOrEqual(cDate, aDate));
  73. },
  74. /**
  75. * Adds a specified number of days to the given date.
  76. * @for Date
  77. * @method addDays
  78. * @param oDate {Date} The date to add days to.
  79. * @param numDays {Number} The number of days to add (can be negative)
  80. * @return {Date} A new Date with the specified number of days
  81. * added to the original date.
  82. */
  83. addDays : function (oDate, numDays) {
  84. return new Date(oDate.getTime() + 86400000*numDays);
  85. },
  86. /**
  87. * Adds a specified number of months to the given date.
  88. * @for Date
  89. * @method addMonths
  90. * @param oDate {Date} The date to add months to.
  91. * @param numMonths {Number} The number of months to add (can be negative)
  92. * @return {Date} A new Date with the specified number of months
  93. * added to the original date.
  94. */
  95. addMonths : function (oDate, numMonths) {
  96. var newYear = oDate.getFullYear();
  97. var newMonth = oDate.getMonth() + numMonths;
  98. newYear = Math.floor(newYear + newMonth / 12);
  99. newMonth = (newMonth % 12 + 12) % 12;
  100. var newDate = new Date (oDate.getTime());
  101. newDate.setFullYear(newYear);
  102. newDate.setMonth(newMonth);
  103. return newDate;
  104. },
  105. /**
  106. * Adds a specified number of years to the given date.
  107. * @for Date
  108. * @method addYears
  109. * @param oDate {Date} The date to add years to.
  110. * @param numYears {Number} The number of years to add (can be negative)
  111. * @return {Date} A new Date with the specified number of years
  112. * added to the original date.
  113. */
  114. addYears : function (oDate, numYears) {
  115. var newYear = oDate.getFullYear() + numYears;
  116. var newDate = new Date(oDate.getTime());
  117. newDate.setFullYear(newYear);
  118. return newDate;
  119. },
  120. /**
  121. * Lists all dates in a given month.
  122. * @for Date
  123. * @method listOfDatesInMonth
  124. * @param oDate {Date} The date corresponding to the month for
  125. * which a list of dates is required.
  126. * @return {Array} An `Array` of `Date`s from a given month.
  127. */
  128. listOfDatesInMonth : function (oDate) {
  129. if (!this.isValidDate(oDate)) {
  130. return [];
  131. }
  132. var daysInMonth = this.daysInMonth(oDate),
  133. year = oDate.getFullYear(),
  134. month = oDate.getMonth(),
  135. output = [];
  136. for (var day = 1; day <= daysInMonth; day++) {
  137. output.push(new Date(year, month, day, 12, 0, 0));
  138. }
  139. return output;
  140. },
  141. /**
  142. * Takes a native JavaScript Date and returns the number of days
  143. * in the month that the given date belongs to.
  144. * @for Date
  145. * @method daysInMonth
  146. * @param oDate {Date} Date in the month for which the number
  147. * of days is desired.
  148. * @return {Number} A number (either 28, 29, 30 or 31) of days
  149. * in the given month.
  150. */
  151. daysInMonth : function (oDate) {
  152. if (!this.isValidDate(oDate)) {
  153. return 0;
  154. }
  155. var mon = oDate.getMonth();
  156. var lengths = [31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31];
  157. if (mon != 1) {
  158. return lengths[mon];
  159. }
  160. else {
  161. var year = oDate.getFullYear();
  162. if (year%400 === 0) {
  163. return 29;
  164. }
  165. else if (year%100 === 0) {
  166. return 28;
  167. }
  168. else if (year%4 === 0) {
  169. return 29;
  170. }
  171. else {
  172. return 28;
  173. }
  174. }
  175. }
  176. });
  177. Y.namespace("DataType");
  178. Y.DataType.Date = Y.Date;