date.js 9.5 KB

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