[ Index ]

PHP Cross Reference of Unnamed Project

title

Body

[close]

/lib/yuilib/3.17.2/datatype-date-math/ -> datatype-date-math-debug.js (source)

   1  /*
   2  YUI 3.17.2 (build 9c3c78e)
   3  Copyright 2014 Yahoo! Inc. All rights reserved.
   4  Licensed under the BSD License.
   5  http://yuilibrary.com/license/
   6  */
   7  
   8  YUI.add('datatype-date-math', function (Y, NAME) {
   9  
  10  /**
  11   * Date Math submodule.
  12   *
  13   * @module datatype-date
  14   * @submodule datatype-date-math
  15   * @for Date
  16   */
  17  var LANG = Y.Lang;
  18  
  19  Y.mix(Y.namespace("Date"), {
  20  
  21      /**
  22       * Checks whether a native JavaScript Date contains a valid value.
  23       * @for Date
  24       * @method isValidDate
  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.
  27       */
  28       isValidDate : function (oDate) {
  29          if(LANG.isDate(oDate) && (isFinite(oDate)) && (oDate != "Invalid Date") && !isNaN(oDate) && (oDate != null)) {
  30              return true;
  31          }
  32          else {
  33              Y.log("Could not validate data as type Date", "warn", "date");
  34              return false;
  35          }
  36      },
  37  
  38      /**
  39       * Checks whether two dates correspond to the same date and time.
  40       * @for Date
  41       * @method areEqual
  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
  45       * date and time.
  46       */
  47      areEqual : function (aDate, bDate) {
  48          return (this.isValidDate(aDate) && this.isValidDate(bDate) && (aDate.getTime() == bDate.getTime()));
  49      },
  50  
  51      /**
  52       * Checks whether the first date comes later than the second.
  53       * @for Date
  54       * @method isGreater
  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.
  58       */
  59      isGreater : function (aDate, bDate) {
  60          return (this.isValidDate(aDate) && this.isValidDate(bDate) && (aDate.getTime() > bDate.getTime()));
  61      },
  62  
  63      /**
  64       * Checks whether the first date comes later than or is the same as
  65       * the second.
  66       * @for Date
  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.
  72       */
  73      isGreaterOrEqual : function (aDate, bDate) {
  74          return (this.isValidDate(aDate) && this.isValidDate(bDate) && (aDate.getTime() >= bDate.getTime()));
  75      },
  76  
  77  
  78      /**
  79       * Checks whether the date is between two other given dates.
  80       * @for Date
  81       * @method isInRange
  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.
  86       */
  87      isInRange : function (aDate, bDate, cDate) {
  88          return (this.isGreaterOrEqual(aDate, bDate) && this.isGreaterOrEqual(cDate, aDate));
  89      },
  90  
  91      /**
  92       * Adds a specified number of days to the given date.
  93       * @for Date
  94       * @method addDays
  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.
  99       */
 100      addDays : function (oDate, numDays) {
 101          return new Date(oDate.getTime() + 86400000*numDays);
 102      },
 103  
 104  
 105      /**
 106       * Adds a specified number of months to the given date.
 107       * @for Date
 108       * @method addMonths
 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.
 113       */
 114      addMonths : function (oDate, numMonths) {
 115          var newYear = oDate.getFullYear();
 116          var newMonth = oDate.getMonth() + numMonths;
 117  
 118          newYear  = Math.floor(newYear + newMonth / 12);
 119          newMonth = (newMonth % 12 + 12) % 12;
 120  
 121          var newDate = new Date (oDate.getTime());
 122          newDate.setFullYear(newYear);
 123          newDate.setMonth(newMonth);
 124  
 125          return newDate;
 126      },
 127  
 128      /**
 129       * Adds a specified number of years to the given date.
 130       * @for Date
 131       * @method addYears
 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.
 136       */
 137      addYears : function (oDate, numYears) {
 138          var newYear = oDate.getFullYear() + numYears;
 139          var newDate = new Date(oDate.getTime());
 140  
 141          newDate.setFullYear(newYear);
 142          return newDate;
 143      },
 144  
 145      /**
 146       * Lists all dates in a given month.
 147       * @for Date
 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.
 152       */
 153      listOfDatesInMonth : function (oDate) {
 154         if (!this.isValidDate(oDate)) {
 155           return [];
 156         }
 157  
 158         var daysInMonth = this.daysInMonth(oDate),
 159             year        = oDate.getFullYear(),
 160             month       = oDate.getMonth(),
 161             output      = [];
 162  
 163         for (var day = 1; day <= daysInMonth; day++) {
 164             output.push(new Date(year, month, day, 12, 0, 0));
 165         }
 166  
 167         return output;
 168      },
 169  
 170      /**
 171       * Takes a native JavaScript Date and returns the number of days
 172       * in the month that the given date belongs to.
 173       * @for Date
 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.
 179       */
 180       daysInMonth : function (oDate) {
 181          if (!this.isValidDate(oDate)) {
 182              return 0;
 183          }
 184  
 185          var mon = oDate.getMonth();
 186          var lengths = [31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31];
 187  
 188          if (mon != 1) {
 189              return lengths[mon];
 190          }
 191          else {
 192  
 193              var year = oDate.getFullYear();
 194              if (year%400 === 0) {
 195                     return 29;
 196              }
 197              else if (year%100 === 0) {
 198                     return 28;
 199              }
 200              else if (year%4 === 0) {
 201                     return 29;
 202              }
 203              else {
 204                     return 28;
 205              }
 206         }
 207      }
 208  
 209  });
 210  
 211  Y.namespace("DataType");
 212  Y.DataType.Date = Y.Date;
 213  
 214  
 215  }, '3.17.2', {"requires": ["yui-base"]});


Generated: Thu Aug 11 10:00:09 2016 Cross-referenced by PHPXref 0.7.1