1 if(!dojo._hasResource["dojo.date"]){ //_hasResource checks added by build. Do not use _hasResource directly in your code.
2 dojo._hasResource["dojo.date"] = true;
3 dojo.provide("dojo.date");
7 // summary: Date manipulation utilities
11 dojo.date.getDaysInMonth = function(/*Date*/dateObject){
13 // Returns the number of days in the month used by dateObject
14 var month = dateObject.getMonth();
15 var days = [31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31];
16 if(month == 1 && dojo.date.isLeapYear(dateObject)){ return 29; } // Number
17 return days[month]; // Number
20 dojo.date.isLeapYear = function(/*Date*/dateObject){
22 // Determines if the year of the dateObject is a leap year
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
30 var year = dateObject.getFullYear();
31 return !(year%400) || (!(year%4) && !!(year%100)); // Boolean
34 // FIXME: This is not localized
35 dojo.date.getTimezoneName = function(/*Date*/dateObject){
37 // Get the user's time zone as provided by the browser
39 // Needed because the timezone may vary with time (daylight savings)
41 // Try to get time zone info from toString or toLocaleString method of
42 // the Date object -- UTC offset is not a time zone. See
43 // http://www.twinsun.com/tz/tz-link.htm Note: results may be
44 // inconsistent across browsers.
46 var str = dateObject.toString(); // Start looking in toString
47 var tz = ''; // The result -- return empty string if nothing found
50 // First look for something in parentheses -- fast lookup, no regex
51 var pos = str.indexOf('(');
53 tz = str.substring(++pos, str.indexOf(')'));
55 // If at first you don't succeed ...
56 // If IE knows about the TZ, it appears before the year
57 // Capital letters or slash before a 4-digit year
58 // at the end of string
59 var pat = /([A-Z\/]+) \d{4}$/;
60 if((match = str.match(pat))){
63 // Some browsers (e.g. Safari) glue the TZ on the end
64 // of toLocaleString instead of putting it in toString
65 str = dateObject.toLocaleString();
66 // Capital letters or slash -- end of string,
69 if((match = str.match(pat))){
75 // Make sure it doesn't somehow end up return AM or PM
76 return (tz == 'AM' || tz == 'PM') ? '' : tz; // String
79 // Utility methods to do arithmetic calculations with Dates
81 dojo.date.compare = function(/*Date*/date1, /*Date?*/date2, /*String?*/portion){
83 // Compare two date objects by date, time, or both.
85 // Returns 0 if equal, positive if a > b, else negative.
89 // Date object. If not specified, the current Date is used.
91 // A string indicating the "date" or "time" portion of a Date object.
92 // Compares both "date" and "time" by default. One of the following:
93 // "date", "time", "datetime"
95 // Extra step required in copy for IE - see #3112
96 date1 = new Date(Number(date1));
97 date2 = new Date(Number(date2 || new Date()));
99 if(portion !== "undefined"){
100 if(portion == "date"){
101 // Ignore times and compare dates.
102 date1.setHours(0, 0, 0, 0);
103 date2.setHours(0, 0, 0, 0);
104 }else if(portion == "time"){
105 // Ignore dates and compare times.
106 date1.setFullYear(0, 0, 0);
107 date2.setFullYear(0, 0, 0);
111 if(date1 > date2){ return 1; } // int
112 if(date1 < date2){ return -1; } // int
116 dojo.date.add = function(/*Date*/date, /*String*/interval, /*int*/amount){
118 // Add to a Date in intervals of different size, from milliseconds to years
120 // Date object to start with
122 // A string representing the interval. One of the following:
123 // "year", "month", "day", "hour", "minute", "second",
124 // "millisecond", "quarter", "week", "weekday"
126 // How much to add to the date.
128 var sum = new Date(Number(date)); // convert to Number before copying to accomodate IE (#3112)
129 var fixOvershoot = false;
130 var property = "Date";
136 //i18n FIXME: assumes Saturday/Sunday weekend, but this is not always true. see dojo.cldr.supplemental
138 // Divide the increment time span into weekspans plus leftover days
139 // e.g., 8 days is one 5-day weekspan / and two leftover days
140 // Can't have zero leftover days, so numbers divisible by 5 get
141 // a days value of 5, and the remaining days make up the number of weeks
143 var mod = amount % 5;
145 days = (amount > 0) ? 5 : -5;
146 weeks = (amount > 0) ? ((amount-5)/5) : ((amount+5)/5);
149 weeks = parseInt(amount/5);
151 // Get weekday value for orig date param
152 var strt = date.getDay();
153 // Orig date is Sat / positive incrementer
156 if(strt == 6 && amount > 0){
158 }else if(strt == 0 && amount < 0){
159 // Orig date is Sun / negative incrementer
160 // Jump back over Sat
163 // Get weekday val for the new date
164 var trgt = strt + days;
165 // New date is on Sat or Sun
166 if(trgt == 0 || trgt == 6){
167 adj = (amount > 0) ? 2 : -2;
169 // Increment by number of weeks plus leftover days plus
170 // weekend adjustments
171 amount = (7 * weeks) + days + adj;
174 property = "FullYear";
175 // Keep increment/decrement from 2/29 out of March
182 // Naive quarter is just three months
186 // Reset to last day of month if you overshoot
194 property = "UTC"+interval.charAt(0).toUpperCase() + interval.substring(1) + "s";
198 sum["set"+property](sum["get"+property]()+amount);
201 if(fixOvershoot && (sum.getDate() < date.getDate())){
208 dojo.date.difference = function(/*Date*/date1, /*Date?*/date2, /*String?*/interval){
210 // Get the difference in a specific unit of time (e.g., number of
211 // months, weeks, days, etc.) between two dates, rounded to the
216 // Date object. If not specified, the current Date is used.
218 // A string representing the interval. One of the following:
219 // "year", "month", "day", "hour", "minute", "second",
220 // "millisecond", "quarter", "week", "weekday"
221 // Defaults to "day".
223 date2 = date2 || new Date();
224 interval = interval || "day";
225 var yearDiff = date2.getFullYear() - date1.getFullYear();
226 var delta = 1; // Integer return value
230 var m1 = date1.getMonth();
231 var m2 = date2.getMonth();
232 // Figure out which quarter the months are in
233 var q1 = Math.floor(m1/3) + 1;
234 var q2 = Math.floor(m2/3) + 1;
235 // Add quarters for any year difference between the dates
236 q2 += (yearDiff * 4);
240 var days = Math.round(dojo.date.difference(date1, date2, "day"));
241 var weeks = parseInt(dojo.date.difference(date1, date2, "week"));
244 // Even number of weeks
248 // Weeks plus spare change (< 7 days)
250 var aDay = date1.getDay();
251 var bDay = date2.getDay();
253 weeks = parseInt(days/7);
255 // Mark the date advanced by the number of
256 // round weeks (may be zero)
257 var dtMark = new Date(date1);
258 dtMark.setDate(dtMark.getDate()+(weeks*7));
259 var dayMark = dtMark.getDay();
261 // Spare change days -- 6 or less
264 // Range starts on Sat
268 // Range starts on Sun
280 // Range contains weekend
281 case (dayMark + mod) > 5:
286 // Range starts on Sat
290 // Range starts on Sun
302 // Range contains weekend
303 case (dayMark + mod) < 0:
316 delta = (date2.getMonth() - date1.getMonth()) + (yearDiff * 12);
319 // Truncate instead of rounding
320 // Don't use Math.floor -- value may be negative
321 delta = parseInt(dojo.date.difference(date1, date2, "day")/7);
336 delta *= date2.getTime() - date1.getTime();
339 // Round for fractional values and DST leaps
340 return Math.round(delta); // Number (integer)