date.js 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351
  1. /*
  2. Copyright (c) 2004-2012, The Dojo Foundation All Rights Reserved.
  3. Available via Academic Free License >= 2.1 OR the modified BSD license.
  4. see: http://dojotoolkit.org/license for details
  5. */
  6. if(!dojo._hasResource["dojo.date"]){ //_hasResource checks added by build. Do not use _hasResource directly in your code.
  7. dojo._hasResource["dojo.date"] = true;
  8. dojo.provide("dojo.date");
  9. dojo.getObject("date", true, dojo);
  10. /*=====
  11. dojo.date = {
  12. // summary: Date manipulation utilities
  13. }
  14. =====*/
  15. dojo.date.getDaysInMonth = function(/*Date*/dateObject){
  16. // summary:
  17. // Returns the number of days in the month used by dateObject
  18. var month = dateObject.getMonth();
  19. var days = [31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31];
  20. if(month == 1 && dojo.date.isLeapYear(dateObject)){ return 29; } // Number
  21. return days[month]; // Number
  22. };
  23. dojo.date.isLeapYear = function(/*Date*/dateObject){
  24. // summary:
  25. // Determines if the year of the dateObject is a leap year
  26. // description:
  27. // Leap years are years with an additional day YYYY-02-29, where the
  28. // year number is a multiple of four with the following exception: If
  29. // a year is a multiple of 100, then it is only a leap year if it is
  30. // also a multiple of 400. For example, 1900 was not a leap year, but
  31. // 2000 is one.
  32. var year = dateObject.getFullYear();
  33. return !(year%400) || (!(year%4) && !!(year%100)); // Boolean
  34. };
  35. // FIXME: This is not localized
  36. dojo.date.getTimezoneName = function(/*Date*/dateObject){
  37. // summary:
  38. // Get the user's time zone as provided by the browser
  39. // dateObject:
  40. // Needed because the timezone may vary with time (daylight savings)
  41. // description:
  42. // Try to get time zone info from toString or toLocaleString method of
  43. // the Date object -- UTC offset is not a time zone. See
  44. // http://www.twinsun.com/tz/tz-link.htm Note: results may be
  45. // inconsistent across browsers.
  46. var str = dateObject.toString(); // Start looking in toString
  47. var tz = ''; // The result -- return empty string if nothing found
  48. var match;
  49. // First look for something in parentheses -- fast lookup, no regex
  50. var pos = str.indexOf('(');
  51. if(pos > -1){
  52. tz = str.substring(++pos, str.indexOf(')'));
  53. }else{
  54. // If at first you don't succeed ...
  55. // If IE knows about the TZ, it appears before the year
  56. // Capital letters or slash before a 4-digit year
  57. // at the end of string
  58. var pat = /([A-Z\/]+) \d{4}$/;
  59. if((match = str.match(pat))){
  60. tz = match[1];
  61. }else{
  62. // Some browsers (e.g. Safari) glue the TZ on the end
  63. // of toLocaleString instead of putting it in toString
  64. str = dateObject.toLocaleString();
  65. // Capital letters or slash -- end of string,
  66. // after space
  67. pat = / ([A-Z\/]+)$/;
  68. if((match = str.match(pat))){
  69. tz = match[1];
  70. }
  71. }
  72. }
  73. // Make sure it doesn't somehow end up return AM or PM
  74. return (tz == 'AM' || tz == 'PM') ? '' : tz; // String
  75. };
  76. // Utility methods to do arithmetic calculations with Dates
  77. dojo.date.compare = function(/*Date*/date1, /*Date?*/date2, /*String?*/portion){
  78. // summary:
  79. // Compare two date objects by date, time, or both.
  80. // description:
  81. // Returns 0 if equal, positive if a > b, else negative.
  82. // date1:
  83. // Date object
  84. // date2:
  85. // Date object. If not specified, the current Date is used.
  86. // portion:
  87. // A string indicating the "date" or "time" portion of a Date object.
  88. // Compares both "date" and "time" by default. One of the following:
  89. // "date", "time", "datetime"
  90. // Extra step required in copy for IE - see #3112
  91. date1 = new Date(+date1);
  92. date2 = new Date(+(date2 || new Date()));
  93. if(portion == "date"){
  94. // Ignore times and compare dates.
  95. date1.setHours(0, 0, 0, 0);
  96. date2.setHours(0, 0, 0, 0);
  97. }else if(portion == "time"){
  98. // Ignore dates and compare times.
  99. date1.setFullYear(0, 0, 0);
  100. date2.setFullYear(0, 0, 0);
  101. }
  102. if(date1 > date2){ return 1; } // int
  103. if(date1 < date2){ return -1; } // int
  104. return 0; // int
  105. };
  106. dojo.date.add = function(/*Date*/date, /*String*/interval, /*int*/amount){
  107. // summary:
  108. // Add to a Date in intervals of different size, from milliseconds to years
  109. // date: Date
  110. // Date object to start with
  111. // interval:
  112. // A string representing the interval. One of the following:
  113. // "year", "month", "day", "hour", "minute", "second",
  114. // "millisecond", "quarter", "week", "weekday"
  115. // amount:
  116. // How much to add to the date.
  117. var sum = new Date(+date); // convert to Number before copying to accomodate IE (#3112)
  118. var fixOvershoot = false;
  119. var property = "Date";
  120. switch(interval){
  121. case "day":
  122. break;
  123. case "weekday":
  124. //i18n FIXME: assumes Saturday/Sunday weekend, but this is not always true. see dojo.cldr.supplemental
  125. // Divide the increment time span into weekspans plus leftover days
  126. // e.g., 8 days is one 5-day weekspan / and two leftover days
  127. // Can't have zero leftover days, so numbers divisible by 5 get
  128. // a days value of 5, and the remaining days make up the number of weeks
  129. var days, weeks;
  130. var mod = amount % 5;
  131. if(!mod){
  132. days = (amount > 0) ? 5 : -5;
  133. weeks = (amount > 0) ? ((amount-5)/5) : ((amount+5)/5);
  134. }else{
  135. days = mod;
  136. weeks = parseInt(amount/5);
  137. }
  138. // Get weekday value for orig date param
  139. var strt = date.getDay();
  140. // Orig date is Sat / positive incrementer
  141. // Jump over Sun
  142. var adj = 0;
  143. if(strt == 6 && amount > 0){
  144. adj = 1;
  145. }else if(strt == 0 && amount < 0){
  146. // Orig date is Sun / negative incrementer
  147. // Jump back over Sat
  148. adj = -1;
  149. }
  150. // Get weekday val for the new date
  151. var trgt = strt + days;
  152. // New date is on Sat or Sun
  153. if(trgt == 0 || trgt == 6){
  154. adj = (amount > 0) ? 2 : -2;
  155. }
  156. // Increment by number of weeks plus leftover days plus
  157. // weekend adjustments
  158. amount = (7 * weeks) + days + adj;
  159. break;
  160. case "year":
  161. property = "FullYear";
  162. // Keep increment/decrement from 2/29 out of March
  163. fixOvershoot = true;
  164. break;
  165. case "week":
  166. amount *= 7;
  167. break;
  168. case "quarter":
  169. // Naive quarter is just three months
  170. amount *= 3;
  171. // fallthrough...
  172. case "month":
  173. // Reset to last day of month if you overshoot
  174. fixOvershoot = true;
  175. property = "Month";
  176. break;
  177. // case "hour":
  178. // case "minute":
  179. // case "second":
  180. // case "millisecond":
  181. default:
  182. property = "UTC"+interval.charAt(0).toUpperCase() + interval.substring(1) + "s";
  183. }
  184. if(property){
  185. sum["set"+property](sum["get"+property]()+amount);
  186. }
  187. if(fixOvershoot && (sum.getDate() < date.getDate())){
  188. sum.setDate(0);
  189. }
  190. return sum; // Date
  191. };
  192. dojo.date.difference = function(/*Date*/date1, /*Date?*/date2, /*String?*/interval){
  193. // summary:
  194. // Get the difference in a specific unit of time (e.g., number of
  195. // months, weeks, days, etc.) between two dates, rounded to the
  196. // nearest integer.
  197. // date1:
  198. // Date object
  199. // date2:
  200. // Date object. If not specified, the current Date is used.
  201. // interval:
  202. // A string representing the interval. One of the following:
  203. // "year", "month", "day", "hour", "minute", "second",
  204. // "millisecond", "quarter", "week", "weekday"
  205. // Defaults to "day".
  206. date2 = date2 || new Date();
  207. interval = interval || "day";
  208. var yearDiff = date2.getFullYear() - date1.getFullYear();
  209. var delta = 1; // Integer return value
  210. switch(interval){
  211. case "quarter":
  212. var m1 = date1.getMonth();
  213. var m2 = date2.getMonth();
  214. // Figure out which quarter the months are in
  215. var q1 = Math.floor(m1/3) + 1;
  216. var q2 = Math.floor(m2/3) + 1;
  217. // Add quarters for any year difference between the dates
  218. q2 += (yearDiff * 4);
  219. delta = q2 - q1;
  220. break;
  221. case "weekday":
  222. var days = Math.round(dojo.date.difference(date1, date2, "day"));
  223. var weeks = parseInt(dojo.date.difference(date1, date2, "week"));
  224. var mod = days % 7;
  225. // Even number of weeks
  226. if(mod == 0){
  227. days = weeks*5;
  228. }else{
  229. // Weeks plus spare change (< 7 days)
  230. var adj = 0;
  231. var aDay = date1.getDay();
  232. var bDay = date2.getDay();
  233. weeks = parseInt(days/7);
  234. mod = days % 7;
  235. // Mark the date advanced by the number of
  236. // round weeks (may be zero)
  237. var dtMark = new Date(date1);
  238. dtMark.setDate(dtMark.getDate()+(weeks*7));
  239. var dayMark = dtMark.getDay();
  240. // Spare change days -- 6 or less
  241. if(days > 0){
  242. switch(true){
  243. // Range starts on Sat
  244. case aDay == 6:
  245. adj = -1;
  246. break;
  247. // Range starts on Sun
  248. case aDay == 0:
  249. adj = 0;
  250. break;
  251. // Range ends on Sat
  252. case bDay == 6:
  253. adj = -1;
  254. break;
  255. // Range ends on Sun
  256. case bDay == 0:
  257. adj = -2;
  258. break;
  259. // Range contains weekend
  260. case (dayMark + mod) > 5:
  261. adj = -2;
  262. }
  263. }else if(days < 0){
  264. switch(true){
  265. // Range starts on Sat
  266. case aDay == 6:
  267. adj = 0;
  268. break;
  269. // Range starts on Sun
  270. case aDay == 0:
  271. adj = 1;
  272. break;
  273. // Range ends on Sat
  274. case bDay == 6:
  275. adj = 2;
  276. break;
  277. // Range ends on Sun
  278. case bDay == 0:
  279. adj = 1;
  280. break;
  281. // Range contains weekend
  282. case (dayMark + mod) < 0:
  283. adj = 2;
  284. }
  285. }
  286. days += adj;
  287. days -= (weeks*2);
  288. }
  289. delta = days;
  290. break;
  291. case "year":
  292. delta = yearDiff;
  293. break;
  294. case "month":
  295. delta = (date2.getMonth() - date1.getMonth()) + (yearDiff * 12);
  296. break;
  297. case "week":
  298. // Truncate instead of rounding
  299. // Don't use Math.floor -- value may be negative
  300. delta = parseInt(dojo.date.difference(date1, date2, "day")/7);
  301. break;
  302. case "day":
  303. delta /= 24;
  304. // fallthrough
  305. case "hour":
  306. delta /= 60;
  307. // fallthrough
  308. case "minute":
  309. delta /= 60;
  310. // fallthrough
  311. case "second":
  312. delta /= 1000;
  313. // fallthrough
  314. case "millisecond":
  315. delta *= date2.getTime() - date1.getTime();
  316. }
  317. // Round for fractional values and DST leaps
  318. return Math.round(delta); // Number (integer)
  319. };
  320. }