API Docs for: 3.10.3
Show:

File: date/js/date-math.js

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