datemath.js 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409
  1. /*
  2. Copyright (c) 2009, Yahoo! Inc. All rights reserved.
  3. Code licensed under the BSD License:
  4. http://developer.yahoo.net/yui/license.txt
  5. version: 2.8.0r4
  6. */
  7. /**
  8. * The datemath module provides utility methods for basic JavaScript Date object manipulation and
  9. * comparison.
  10. *
  11. * @module datemath
  12. */
  13. /**
  14. * YAHOO.widget.DateMath is used for simple date manipulation. The class is a static utility
  15. * used for adding, subtracting, and comparing dates.
  16. * @namespace YAHOO.widget
  17. * @class DateMath
  18. */
  19. YAHOO.widget.DateMath = {
  20. /**
  21. * Constant field representing Day
  22. * @property DAY
  23. * @static
  24. * @final
  25. * @type String
  26. */
  27. DAY : "D",
  28. /**
  29. * Constant field representing Week
  30. * @property WEEK
  31. * @static
  32. * @final
  33. * @type String
  34. */
  35. WEEK : "W",
  36. /**
  37. * Constant field representing Year
  38. * @property YEAR
  39. * @static
  40. * @final
  41. * @type String
  42. */
  43. YEAR : "Y",
  44. /**
  45. * Constant field representing Month
  46. * @property MONTH
  47. * @static
  48. * @final
  49. * @type String
  50. */
  51. MONTH : "M",
  52. /**
  53. * Constant field representing one day, in milliseconds
  54. * @property ONE_DAY_MS
  55. * @static
  56. * @final
  57. * @type Number
  58. */
  59. ONE_DAY_MS : 1000*60*60*24,
  60. /**
  61. * Constant field representing the date in first week of January
  62. * which identifies the first week of the year.
  63. * <p>
  64. * In the U.S, Jan 1st is normally used based on a Sunday start of week.
  65. * ISO 8601, used widely throughout Europe, uses Jan 4th, based on a Monday start of week.
  66. * </p>
  67. * @property WEEK_ONE_JAN_DATE
  68. * @static
  69. * @type Number
  70. */
  71. WEEK_ONE_JAN_DATE : 1,
  72. /**
  73. * Adds the specified amount of time to the this instance.
  74. * @method add
  75. * @param {Date} date The JavaScript Date object to perform addition on
  76. * @param {String} field The field constant to be used for performing addition.
  77. * @param {Number} amount The number of units (measured in the field constant) to add to the date.
  78. * @return {Date} The resulting Date object
  79. */
  80. add : function(date, field, amount) {
  81. var d = new Date(date.getTime());
  82. switch (field) {
  83. case this.MONTH:
  84. var newMonth = date.getMonth() + amount;
  85. var years = 0;
  86. if (newMonth < 0) {
  87. while (newMonth < 0) {
  88. newMonth += 12;
  89. years -= 1;
  90. }
  91. } else if (newMonth > 11) {
  92. while (newMonth > 11) {
  93. newMonth -= 12;
  94. years += 1;
  95. }
  96. }
  97. d.setMonth(newMonth);
  98. d.setFullYear(date.getFullYear() + years);
  99. break;
  100. case this.DAY:
  101. this._addDays(d, amount);
  102. // d.setDate(date.getDate() + amount);
  103. break;
  104. case this.YEAR:
  105. d.setFullYear(date.getFullYear() + amount);
  106. break;
  107. case this.WEEK:
  108. this._addDays(d, (amount * 7));
  109. // d.setDate(date.getDate() + (amount * 7));
  110. break;
  111. }
  112. return d;
  113. },
  114. /**
  115. * Private helper method to account for bug in Safari 2 (webkit < 420)
  116. * when Date.setDate(n) is called with n less than -128 or greater than 127.
  117. * <p>
  118. * Fix approach and original findings are available here:
  119. * http://brianary.blogspot.com/2006/03/safari-date-bug.html
  120. * </p>
  121. * @method _addDays
  122. * @param {Date} d JavaScript date object
  123. * @param {Number} nDays The number of days to add to the date object (can be negative)
  124. * @private
  125. */
  126. _addDays : function(d, nDays) {
  127. if (YAHOO.env.ua.webkit && YAHOO.env.ua.webkit < 420) {
  128. if (nDays < 0) {
  129. // Ensure we don't go below -128 (getDate() is always 1 to 31, so we won't go above 127)
  130. for(var min = -128; nDays < min; nDays -= min) {
  131. d.setDate(d.getDate() + min);
  132. }
  133. } else {
  134. // Ensure we don't go above 96 + 31 = 127
  135. for(var max = 96; nDays > max; nDays -= max) {
  136. d.setDate(d.getDate() + max);
  137. }
  138. }
  139. // nDays should be remainder between -128 and 96
  140. }
  141. d.setDate(d.getDate() + nDays);
  142. },
  143. /**
  144. * Subtracts the specified amount of time from the this instance.
  145. * @method subtract
  146. * @param {Date} date The JavaScript Date object to perform subtraction on
  147. * @param {Number} field The this field constant to be used for performing subtraction.
  148. * @param {Number} amount The number of units (measured in the field constant) to subtract from the date.
  149. * @return {Date} The resulting Date object
  150. */
  151. subtract : function(date, field, amount) {
  152. return this.add(date, field, (amount*-1));
  153. },
  154. /**
  155. * Determines whether a given date is before another date on the calendar.
  156. * @method before
  157. * @param {Date} date The Date object to compare with the compare argument
  158. * @param {Date} compareTo The Date object to use for the comparison
  159. * @return {Boolean} true if the date occurs before the compared date; false if not.
  160. */
  161. before : function(date, compareTo) {
  162. var ms = compareTo.getTime();
  163. if (date.getTime() < ms) {
  164. return true;
  165. } else {
  166. return false;
  167. }
  168. },
  169. /**
  170. * Determines whether a given date is after another date on the calendar.
  171. * @method after
  172. * @param {Date} date The Date object to compare with the compare argument
  173. * @param {Date} compareTo The Date object to use for the comparison
  174. * @return {Boolean} true if the date occurs after the compared date; false if not.
  175. */
  176. after : function(date, compareTo) {
  177. var ms = compareTo.getTime();
  178. if (date.getTime() > ms) {
  179. return true;
  180. } else {
  181. return false;
  182. }
  183. },
  184. /**
  185. * Determines whether a given date is between two other dates on the calendar.
  186. * @method between
  187. * @param {Date} date The date to check for
  188. * @param {Date} dateBegin The start of the range
  189. * @param {Date} dateEnd The end of the range
  190. * @return {Boolean} true if the date occurs between the compared dates; false if not.
  191. */
  192. between : function(date, dateBegin, dateEnd) {
  193. if (this.after(date, dateBegin) && this.before(date, dateEnd)) {
  194. return true;
  195. } else {
  196. return false;
  197. }
  198. },
  199. /**
  200. * Retrieves a JavaScript Date object representing January 1 of any given year.
  201. * @method getJan1
  202. * @param {Number} calendarYear The calendar year for which to retrieve January 1
  203. * @return {Date} January 1 of the calendar year specified.
  204. */
  205. getJan1 : function(calendarYear) {
  206. return this.getDate(calendarYear,0,1);
  207. },
  208. /**
  209. * Calculates the number of days the specified date is from January 1 of the specified calendar year.
  210. * Passing January 1 to this function would return an offset value of zero.
  211. * @method getDayOffset
  212. * @param {Date} date The JavaScript date for which to find the offset
  213. * @param {Number} calendarYear The calendar year to use for determining the offset
  214. * @return {Number} The number of days since January 1 of the given year
  215. */
  216. getDayOffset : function(date, calendarYear) {
  217. var beginYear = this.getJan1(calendarYear); // Find the start of the year. This will be in week 1.
  218. // Find the number of days the passed in date is away from the calendar year start
  219. var dayOffset = Math.ceil((date.getTime()-beginYear.getTime()) / this.ONE_DAY_MS);
  220. return dayOffset;
  221. },
  222. /**
  223. * Calculates the week number for the given date. Can currently support standard
  224. * U.S. week numbers, based on Jan 1st defining the 1st week of the year, and
  225. * ISO8601 week numbers, based on Jan 4th defining the 1st week of the year.
  226. *
  227. * @method getWeekNumber
  228. * @param {Date} date The JavaScript date for which to find the week number
  229. * @param {Number} firstDayOfWeek The index of the first day of the week (0 = Sun, 1 = Mon ... 6 = Sat).
  230. * Defaults to 0
  231. * @param {Number} janDate The date in the first week of January which defines week one for the year
  232. * Defaults to the value of YAHOO.widget.DateMath.WEEK_ONE_JAN_DATE, which is 1 (Jan 1st).
  233. * For the U.S, this is normally Jan 1st. ISO8601 uses Jan 4th to define the first week of the year.
  234. *
  235. * @return {Number} The number of the week containing the given date.
  236. */
  237. getWeekNumber : function(date, firstDayOfWeek, janDate) {
  238. // Setup Defaults
  239. firstDayOfWeek = firstDayOfWeek || 0;
  240. janDate = janDate || this.WEEK_ONE_JAN_DATE;
  241. var targetDate = this.clearTime(date),
  242. startOfWeek,
  243. endOfWeek;
  244. if (targetDate.getDay() === firstDayOfWeek) {
  245. startOfWeek = targetDate;
  246. } else {
  247. startOfWeek = this.getFirstDayOfWeek(targetDate, firstDayOfWeek);
  248. }
  249. var startYear = startOfWeek.getFullYear();
  250. // DST shouldn't be a problem here, math is quicker than setDate();
  251. endOfWeek = new Date(startOfWeek.getTime() + 6*this.ONE_DAY_MS);
  252. var weekNum;
  253. if (startYear !== endOfWeek.getFullYear() && endOfWeek.getDate() >= janDate) {
  254. // If years don't match, endOfWeek is in Jan. and if the
  255. // week has WEEK_ONE_JAN_DATE in it, it's week one by definition.
  256. weekNum = 1;
  257. } else {
  258. // Get the 1st day of the 1st week, and
  259. // find how many days away we are from it.
  260. var weekOne = this.clearTime(this.getDate(startYear, 0, janDate)),
  261. weekOneDayOne = this.getFirstDayOfWeek(weekOne, firstDayOfWeek);
  262. // Round days to smoothen out 1 hr DST diff
  263. var daysDiff = Math.round((targetDate.getTime() - weekOneDayOne.getTime())/this.ONE_DAY_MS);
  264. // Calc. Full Weeks
  265. var rem = daysDiff % 7;
  266. var weeksDiff = (daysDiff - rem)/7;
  267. weekNum = weeksDiff + 1;
  268. }
  269. return weekNum;
  270. },
  271. /**
  272. * Get the first day of the week, for the give date.
  273. * @param {Date} dt The date in the week for which the first day is required.
  274. * @param {Number} startOfWeek The index for the first day of the week, 0 = Sun, 1 = Mon ... 6 = Sat (defaults to 0)
  275. * @return {Date} The first day of the week
  276. */
  277. getFirstDayOfWeek : function (dt, startOfWeek) {
  278. startOfWeek = startOfWeek || 0;
  279. var dayOfWeekIndex = dt.getDay(),
  280. dayOfWeek = (dayOfWeekIndex - startOfWeek + 7) % 7;
  281. return this.subtract(dt, this.DAY, dayOfWeek);
  282. },
  283. /**
  284. * Determines if a given week overlaps two different years.
  285. * @method isYearOverlapWeek
  286. * @param {Date} weekBeginDate The JavaScript Date representing the first day of the week.
  287. * @return {Boolean} true if the date overlaps two different years.
  288. */
  289. isYearOverlapWeek : function(weekBeginDate) {
  290. var overlaps = false;
  291. var nextWeek = this.add(weekBeginDate, this.DAY, 6);
  292. if (nextWeek.getFullYear() != weekBeginDate.getFullYear()) {
  293. overlaps = true;
  294. }
  295. return overlaps;
  296. },
  297. /**
  298. * Determines if a given week overlaps two different months.
  299. * @method isMonthOverlapWeek
  300. * @param {Date} weekBeginDate The JavaScript Date representing the first day of the week.
  301. * @return {Boolean} true if the date overlaps two different months.
  302. */
  303. isMonthOverlapWeek : function(weekBeginDate) {
  304. var overlaps = false;
  305. var nextWeek = this.add(weekBeginDate, this.DAY, 6);
  306. if (nextWeek.getMonth() != weekBeginDate.getMonth()) {
  307. overlaps = true;
  308. }
  309. return overlaps;
  310. },
  311. /**
  312. * Gets the first day of a month containing a given date.
  313. * @method findMonthStart
  314. * @param {Date} date The JavaScript Date used to calculate the month start
  315. * @return {Date} The JavaScript Date representing the first day of the month
  316. */
  317. findMonthStart : function(date) {
  318. var start = this.getDate(date.getFullYear(), date.getMonth(), 1);
  319. return start;
  320. },
  321. /**
  322. * Gets the last day of a month containing a given date.
  323. * @method findMonthEnd
  324. * @param {Date} date The JavaScript Date used to calculate the month end
  325. * @return {Date} The JavaScript Date representing the last day of the month
  326. */
  327. findMonthEnd : function(date) {
  328. var start = this.findMonthStart(date);
  329. var nextMonth = this.add(start, this.MONTH, 1);
  330. var end = this.subtract(nextMonth, this.DAY, 1);
  331. return end;
  332. },
  333. /**
  334. * Clears the time fields from a given date, effectively setting the time to 12 noon.
  335. * @method clearTime
  336. * @param {Date} date The JavaScript Date for which the time fields will be cleared
  337. * @return {Date} The JavaScript Date cleared of all time fields
  338. */
  339. clearTime : function(date) {
  340. date.setHours(12,0,0,0);
  341. return date;
  342. },
  343. /**
  344. * Returns a new JavaScript Date object, representing the given year, month and date. Time fields (hr, min, sec, ms) on the new Date object
  345. * are set to 0. The method allows Date instances to be created with the a year less than 100. "new Date(year, month, date)" implementations
  346. * set the year to 19xx if a year (xx) which is less than 100 is provided.
  347. * <p>
  348. * <em>NOTE:</em>Validation on argument values is not performed. It is the caller's responsibility to ensure
  349. * arguments are valid as per the ECMAScript-262 Date object specification for the new Date(year, month[, date]) constructor.
  350. * </p>
  351. * @method getDate
  352. * @param {Number} y Year.
  353. * @param {Number} m Month index from 0 (Jan) to 11 (Dec).
  354. * @param {Number} d (optional) Date from 1 to 31. If not provided, defaults to 1.
  355. * @return {Date} The JavaScript date object with year, month, date set as provided.
  356. */
  357. getDate : function(y, m, d) {
  358. var dt = null;
  359. if (YAHOO.lang.isUndefined(d)) {
  360. d = 1;
  361. }
  362. if (y >= 100) {
  363. dt = new Date(y, m, d);
  364. } else {
  365. dt = new Date();
  366. dt.setFullYear(y);
  367. dt.setMonth(m);
  368. dt.setDate(d);
  369. dt.setHours(0,0,0,0);
  370. }
  371. return dt;
  372. }
  373. };
  374. YAHOO.register("datemath", YAHOO.widget.DateMath, {version: "2.8.0r4", build: "2449"});