fix bullet point issue - with pasting word documents
[roojs1] / Date.js
1 /*
2  * Based on:
3  * Ext JS Library 1.1.1
4  * Copyright(c) 2006-2007, Ext JS, LLC.
5  *
6  * Originally Released Under LGPL - original licence link has changed is not relivant.
7  *
8  * Fork - LGPL
9  * <script type="text/javascript">
10  */
11
12 /**
13  * @class Date
14  *
15  * The date parsing and format syntax is a subset of
16  * <a href="http://www.php.net/date">PHP's date() function</a>, and the formats that are
17  * supported will provide results equivalent to their PHP versions.
18  *
19  * Following is the list of all currently supported formats:
20  *<pre>
21 Sample date:
22 'Wed Jan 10 2007 15:05:01 GMT-0600 (Central Standard Time)'
23
24 Format  Output      Description
25 ------  ----------  --------------------------------------------------------------
26   d      10         Day of the month, 2 digits with leading zeros
27   D      Wed        A textual representation of a day, three letters
28   j      10         Day of the month without leading zeros
29   l      Wednesday  A full textual representation of the day of the week
30   S      th         English ordinal day of month suffix, 2 chars (use with j)
31   w      3          Numeric representation of the day of the week
32   z      9          The julian date, or day of the year (0-365)
33   W      01         ISO-8601 2-digit week number of year, weeks starting on Monday (00-52)
34   F      January    A full textual representation of the month
35   m      01         Numeric representation of a month, with leading zeros
36   M      Jan        Month name abbreviation, three letters
37   n      1          Numeric representation of a month, without leading zeros
38   t      31         Number of days in the given month
39   L      0          Whether it's a leap year (1 if it is a leap year, else 0)
40   Y      2007       A full numeric representation of a year, 4 digits
41   y      07         A two digit representation of a year
42   a      pm         Lowercase Ante meridiem and Post meridiem
43   A      PM         Uppercase Ante meridiem and Post meridiem
44   g      3          12-hour format of an hour without leading zeros
45   G      15         24-hour format of an hour without leading zeros
46   h      03         12-hour format of an hour with leading zeros
47   H      15         24-hour format of an hour with leading zeros
48   i      05         Minutes with leading zeros
49   s      01         Seconds, with leading zeros
50   O      -0600      Difference to Greenwich time (GMT) in hours (Allows +08, without minutes)
51   P      -06:00     Difference to Greenwich time (GMT) with colon between hours and minutes
52   T      CST        Timezone setting of the machine running the code
53   Z      -21600     Timezone offset in seconds (negative if west of UTC, positive if east)
54 </pre>
55  *
56  * Example usage (note that you must escape format specifiers with '\\' to render them as character literals):
57  * <pre><code>
58 var dt = new Date('1/10/2007 03:05:01 PM GMT-0600');
59 document.write(dt.format('Y-m-d'));                         //2007-01-10
60 document.write(dt.format('F j, Y, g:i a'));                 //January 10, 2007, 3:05 pm
61 document.write(dt.format('l, \\t\\he dS of F Y h:i:s A'));  //Wednesday, the 10th of January 2007 03:05:01 PM
62  </code></pre>
63  *
64  * Here are some standard date/time patterns that you might find helpful.  They
65  * are not part of the source of Date.js, but to use them you can simply copy this
66  * block of code into any script that is included after Date.js and they will also become
67  * globally available on the Date object.  Feel free to add or remove patterns as needed in your code.
68  * <pre><code>
69 Date.patterns = {
70     ISO8601Long:"Y-m-d H:i:s",
71     ISO8601Short:"Y-m-d",
72     ShortDate: "n/j/Y",
73     LongDate: "l, F d, Y",
74     FullDateTime: "l, F d, Y g:i:s A",
75     MonthDay: "F d",
76     ShortTime: "g:i A",
77     LongTime: "g:i:s A",
78     SortableDateTime: "Y-m-d\\TH:i:s",
79     UniversalSortableDateTime: "Y-m-d H:i:sO",
80     YearMonth: "F, Y"
81 };
82 </code></pre>
83  *
84  * Example usage:
85  * <pre><code>
86 var dt = new Date();
87 document.write(dt.format(Date.patterns.ShortDate));
88  </code></pre>
89  */
90
91 /*
92  * Most of the date-formatting functions below are the excellent work of Baron Schwartz.
93  * They generate precompiled functions from date formats instead of parsing and
94  * processing the pattern every time you format a date.  These functions are available
95  * on every Date object (any javascript function).
96  *
97  * The original article and download are here:
98  * http://www.xaprb.com/blog/2005/12/12/javascript-closures-for-runtime-efficiency/
99  *
100  */
101  
102  
103  // was in core
104 /**
105  Returns the number of milliseconds between this date and date
106  @param {Date} date (optional) Defaults to now
107  @param {String} interval (optional) Default Date.MILLI, A valid date interval enum value (eg. Date.DAY) 
108  @return {Number} The diff in milliseconds or units of interval
109  @member Date getElapsed
110  */
111 Date.prototype.getElapsed = function(date, interval)
112 {
113     date = date ||  new Date();
114     var ret = Math.abs(date.getTime()-this.getTime());
115     switch (interval) {
116        
117         case  Date.SECOND:
118             return Math.floor(ret / (1000));
119         case  Date.MINUTE:
120             return Math.floor(ret / (1000*60));
121         case  Date.HOUR:
122             return Math.floor(ret / (1000*60*60));
123         case  Date.DAY:
124             return Math.floor(ret / (1000*60*60*24));
125         case  Date.MONTH: // this does not give exact number...??
126             return ((date.format("Y") - this.format("Y")) * 12) + (date.format("m") - this.format("m"));
127         case  Date.YEAR: // this does not give exact number...??
128             return (date.format("Y") - this.format("Y"));
129        
130         case  Date.MILLI:
131         default:
132             return ret;
133     }
134 };
135  
136 // was in date file..
137
138
139 // private
140 Date.parseFunctions = {count:0};
141 // private
142 Date.parseRegexes = [];
143 // private
144 Date.formatFunctions = {count:0};
145
146 // private
147 Date.prototype.dateFormat = function(format) {
148     if (Date.formatFunctions[format] == null) {
149         Date.createNewFormat(format);
150     }
151     var func = Date.formatFunctions[format];
152     return this[func]();
153 };
154
155
156 /**
157  * Formats a date given the supplied format string
158  * @param {String} format The format string
159  * @return {String} The formatted date
160  * @method
161  */
162 Date.prototype.format = Date.prototype.dateFormat;
163
164 // private
165 Date.createNewFormat = function(format) {
166     var funcName = "format" + Date.formatFunctions.count++;
167     Date.formatFunctions[format] = funcName;
168     var code = "Date.prototype." + funcName + " = function(){return ";
169     var special = false;
170     var ch = '';
171     for (var i = 0; i < format.length; ++i) {
172         ch = format.charAt(i);
173         if (!special && ch == "\\") {
174             special = true;
175         }
176         else if (special) {
177             special = false;
178             code += "'" + String.escape(ch) + "' + ";
179         }
180         else {
181             code += Date.getFormatCode(ch);
182         }
183     }
184     /** eval:var:zzzzzzzzzzzzz */
185     eval(code.substring(0, code.length - 3) + ";}");
186 };
187
188 // private
189 Date.getFormatCode = function(character) {
190     switch (character) {
191     case "d":
192         return "String.leftPad(this.getDate(), 2, '0') + ";
193     case "D":
194         return "Date.dayNames[this.getDay()].substring(0, 3) + ";
195     case "j":
196         return "this.getDate() + ";
197     case "l":
198         return "Date.dayNames[this.getDay()] + ";
199     case "S":
200         return "this.getSuffix() + ";
201     case "w":
202         return "this.getDay() + ";
203     case "z":
204         return "this.getDayOfYear() + ";
205     case "W":
206         return "this.getWeekOfYear() + ";
207     case "F":
208         return "Date.monthNames[this.getMonth()] + ";
209     case "m":
210         return "String.leftPad(this.getMonth() + 1, 2, '0') + ";
211     case "M":
212         return "Date.monthNames[this.getMonth()].substring(0, 3) + ";
213     case "n":
214         return "(this.getMonth() + 1) + ";
215     case "t":
216         return "this.getDaysInMonth() + ";
217     case "L":
218         return "(this.isLeapYear() ? 1 : 0) + ";
219     case "Y":
220         return "this.getFullYear() + ";
221     case "y":
222         return "('' + this.getFullYear()).substring(2, 4) + ";
223     case "a":
224         return "(this.getHours() < 12 ? 'am' : 'pm') + ";
225     case "A":
226         return "(this.getHours() < 12 ? 'AM' : 'PM') + ";
227     case "g":
228         return "((this.getHours() % 12) ? this.getHours() % 12 : 12) + ";
229     case "G":
230         return "this.getHours() + ";
231     case "h":
232         return "String.leftPad((this.getHours() % 12) ? this.getHours() % 12 : 12, 2, '0') + ";
233     case "H":
234         return "String.leftPad(this.getHours(), 2, '0') + ";
235     case "i":
236         return "String.leftPad(this.getMinutes(), 2, '0') + ";
237     case "s":
238         return "String.leftPad(this.getSeconds(), 2, '0') + ";
239     case "O":
240         return "this.getGMTOffset() + ";
241     case "P":
242         return "this.getGMTColonOffset() + ";
243     case "T":
244         return "this.getTimezone() + ";
245     case "Z":
246         return "(this.getTimezoneOffset() * -60) + ";
247     default:
248         return "'" + String.escape(character) + "' + ";
249     }
250 };
251
252 /**
253  * Parses the passed string using the specified format. Note that this function expects dates in normal calendar
254  * format, meaning that months are 1-based (1 = January) and not zero-based like in JavaScript dates.  Any part of
255  * the date format that is not specified will default to the current date value for that part.  Time parts can also
256  * be specified, but default to 0.  Keep in mind that the input date string must precisely match the specified format
257  * string or the parse operation will fail.
258  * Example Usage:
259 <pre><code>
260 //dt = Fri May 25 2007 (current date)
261 var dt = new Date();
262
263 //dt = Thu May 25 2006 (today's month/day in 2006)
264 dt = Date.parseDate("2006", "Y");
265
266 //dt = Sun Jan 15 2006 (all date parts specified)
267 dt = Date.parseDate("2006-1-15", "Y-m-d");
268
269 //dt = Sun Jan 15 2006 15:20:01 GMT-0600 (CST)
270 dt = Date.parseDate("2006-1-15 3:20:01 PM", "Y-m-d h:i:s A" );
271 </code></pre>
272  * @param {String} input The unparsed date as a string
273  * @param {String} format The format the date is in
274  * @return {Date} The parsed date
275  * @static
276  */
277 Date.parseDate = function(input, format) {
278     if (Date.parseFunctions[format] == null) {
279         Date.createParser(format);
280     }
281     var func = Date.parseFunctions[format];
282     return Date[func](input);
283 };
284 /**
285  * @private
286  */
287
288 Date.createParser = function(format) {
289     var funcName = "parse" + Date.parseFunctions.count++;
290     var regexNum = Date.parseRegexes.length;
291     var currentGroup = 1;
292     Date.parseFunctions[format] = funcName;
293
294     var code = "Date." + funcName + " = function(input){\n"
295         + "var y = -1, m = -1, d = -1, h = -1, i = -1, s = -1, o, z, v;\n"
296         + "var d = new Date();\n"
297         + "y = d.getFullYear();\n"
298         + "m = d.getMonth();\n"
299         + "d = d.getDate();\n"
300         + "if (typeof(input) !== 'string') { input = input.toString(); }\n"
301         + "var results = input.match(Date.parseRegexes[" + regexNum + "]);\n"
302         + "if (results && results.length > 0) {";
303     var regex = "";
304
305     var special = false;
306     var ch = '';
307     for (var i = 0; i < format.length; ++i) {
308         ch = format.charAt(i);
309         if (!special && ch == "\\") {
310             special = true;
311         }
312         else if (special) {
313             special = false;
314             regex += String.escape(ch);
315         }
316         else {
317             var obj = Date.formatCodeToRegex(ch, currentGroup);
318             currentGroup += obj.g;
319             regex += obj.s;
320             if (obj.g && obj.c) {
321                 code += obj.c;
322             }
323         }
324     }
325
326     code += "if (y >= 0 && m >= 0 && d > 0 && h >= 0 && i >= 0 && s >= 0)\n"
327         + "{v = new Date(y, m, d, h, i, s); v.setFullYear(y);}\n"
328         + "else if (y >= 0 && m >= 0 && d > 0 && h >= 0 && i >= 0)\n"
329         + "{v = new Date(y, m, d, h, i); v.setFullYear(y);}\n"
330         + "else if (y >= 0 && m >= 0 && d > 0 && h >= 0)\n"
331         + "{v = new Date(y, m, d, h); v.setFullYear(y);}\n"
332         + "else if (y >= 0 && m >= 0 && d > 0)\n"
333         + "{v = new Date(y, m, d); v.setFullYear(y);}\n"
334         + "else if (y >= 0 && m >= 0)\n"
335         + "{v = new Date(y, m); v.setFullYear(y);}\n"
336         + "else if (y >= 0)\n"
337         + "{v = new Date(y); v.setFullYear(y);}\n"
338         + "}return (v && (z || o))?\n" // favour UTC offset over GMT offset
339         + "    ((z)? v.add(Date.SECOND, (v.getTimezoneOffset() * 60) + (z*1)) :\n" // reset to UTC, then add offset
340         + "        v.add(Date.HOUR, (v.getGMTOffset() / 100) + (o / -100))) : v\n" // reset to GMT, then add offset
341         + ";}";
342
343     Date.parseRegexes[regexNum] = new RegExp("^" + regex + "$");
344     /** eval:var:zzzzzzzzzzzzz */
345     eval(code);
346 };
347
348 // private
349 Date.formatCodeToRegex = function(character, currentGroup) {
350     switch (character) {
351     case "D":
352         return {g:0,
353         c:null,
354         s:"(?:Sun|Mon|Tue|Wed|Thu|Fri|Sat)"};
355     case "j":
356         return {g:1,
357             c:"d = parseInt(results[" + currentGroup + "], 10);\n",
358             s:"(\\d{1,2})"}; // day of month without leading zeroes
359     case "d":
360         return {g:1,
361             c:"d = parseInt(results[" + currentGroup + "], 10);\n",
362             s:"(\\d{2})"}; // day of month with leading zeroes
363     case "l":
364         return {g:0,
365             c:null,
366             s:"(?:" + Date.dayNames.join("|") + ")"};
367     case "S":
368         return {g:0,
369             c:null,
370             s:"(?:st|nd|rd|th)"};
371     case "w":
372         return {g:0,
373             c:null,
374             s:"\\d"};
375     case "z":
376         return {g:0,
377             c:null,
378             s:"(?:\\d{1,3})"};
379     case "W":
380         return {g:0,
381             c:null,
382             s:"(?:\\d{2})"};
383     case "F":
384         return {g:1,
385             c:"m = parseInt(Date.monthNumbers[results[" + currentGroup + "].substring(0, 3)], 10);\n",
386             s:"(" + Date.monthNames.join("|") + ")"};
387     case "M":
388         return {g:1,
389             c:"m = parseInt(Date.monthNumbers[results[" + currentGroup + "]], 10);\n",
390             s:"(Jan|Feb|Mar|Apr|May|Jun|Jul|Aug|Sep|Oct|Nov|Dec)"};
391     case "n":
392         return {g:1,
393             c:"m = parseInt(results[" + currentGroup + "], 10) - 1;\n",
394             s:"(\\d{1,2})"}; // Numeric representation of a month, without leading zeros
395     case "m":
396         return {g:1,
397             c:"m = Math.max(0,parseInt(results[" + currentGroup + "], 10) - 1);\n",
398             s:"(\\d{2})"}; // Numeric representation of a month, with leading zeros
399     case "t":
400         return {g:0,
401             c:null,
402             s:"\\d{1,2}"};
403     case "L":
404         return {g:0,
405             c:null,
406             s:"(?:1|0)"};
407     case "Y":
408         return {g:1,
409             c:"y = parseInt(results[" + currentGroup + "], 10);\n",
410             s:"(\\d{4})"};
411     case "y":
412         return {g:1,
413             c:"var ty = parseInt(results[" + currentGroup + "], 10);\n"
414                 + "y = ty > Date.y2kYear ? 1900 + ty : 2000 + ty;\n",
415             s:"(\\d{1,2})"};
416     case "a":
417         return {g:1,
418             c:"if (results[" + currentGroup + "] == 'am') {\n"
419                 + "if (h == 12) { h = 0; }\n"
420                 + "} else { if (h < 12) { h += 12; }}",
421             s:"(am|pm)"};
422     case "A":
423         return {g:1,
424             c:"if (results[" + currentGroup + "] == 'AM') {\n"
425                 + "if (h == 12) { h = 0; }\n"
426                 + "} else { if (h < 12) { h += 12; }}",
427             s:"(AM|PM)"};
428     case "g":
429     case "G":
430         return {g:1,
431             c:"h = parseInt(results[" + currentGroup + "], 10);\n",
432             s:"(\\d{1,2})"}; // 12/24-hr format  format of an hour without leading zeroes
433     case "h":
434     case "H":
435         return {g:1,
436             c:"h = parseInt(results[" + currentGroup + "], 10);\n",
437             s:"(\\d{2})"}; //  12/24-hr format  format of an hour with leading zeroes
438     case "i":
439         return {g:1,
440             c:"i = parseInt(results[" + currentGroup + "], 10);\n",
441             s:"(\\d{2})"};
442     case "s":
443         return {g:1,
444             c:"s = parseInt(results[" + currentGroup + "], 10);\n",
445             s:"(\\d{2})"};
446     case "O":
447         return {g:1,
448             c:[
449                 "o = results[", currentGroup, "];\n",
450                 "var sn = o.substring(0,1);\n", // get + / - sign
451                 "var hr = o.substring(1,3)*1 + Math.floor(o.substring(3,5) / 60);\n", // get hours (performs minutes-to-hour conversion also)
452                 "var mn = o.substring(3,5) % 60;\n", // get minutes
453                 "o = ((-12 <= (hr*60 + mn)/60) && ((hr*60 + mn)/60 <= 14))?\n", // -12hrs <= GMT offset <= 14hrs
454                 "    (sn + String.leftPad(hr, 2, 0) + String.leftPad(mn, 2, 0)) : null;\n"
455             ].join(""),
456             s:"([+\-]\\d{2,4})"};
457     
458     
459     case "P":
460         return {g:1,
461                 c:[
462                    "o = results[", currentGroup, "];\n",
463                    "var sn = o.substring(0,1);\n",
464                    "var hr = o.substring(1,3)*1 + Math.floor(o.substring(4,6) / 60);\n",
465                    "var mn = o.substring(4,6) % 60;\n",
466                    "o = ((-12 <= (hr*60 + mn)/60) && ((hr*60 + mn)/60 <= 14))?\n",
467                         "    (sn + String.leftPad(hr, 2, 0) + String.leftPad(mn, 2, 0)) : null;\n"
468             ].join(""),
469             s:"([+\-]\\d{4})"};
470     case "T":
471         return {g:0,
472             c:null,
473             s:"[A-Z]{1,4}"}; // timezone abbrev. may be between 1 - 4 chars
474     case "Z":
475         return {g:1,
476             c:"z = results[" + currentGroup + "];\n" // -43200 <= UTC offset <= 50400
477                   + "z = (-43200 <= z*1 && z*1 <= 50400)? z : null;\n",
478             s:"([+\-]?\\d{1,5})"}; // leading '+' sign is optional for UTC offset
479     default:
480         return {g:0,
481             c:null,
482             s:String.escape(character)};
483     }
484 };
485
486 /**
487  * Get the timezone abbreviation of the current date (equivalent to the format specifier 'T').
488  * @return {String} The abbreviated timezone name (e.g. 'CST')
489  */
490 Date.prototype.getTimezone = function() {
491     return this.toString().replace(/^.*? ([A-Z]{1,4})[\-+][0-9]{4} .*$/, "$1");
492 };
493
494 /**
495  * Get the offset from GMT of the current date (equivalent to the format specifier 'O').
496  * @return {String} The 4-character offset string prefixed with + or - (e.g. '-0600')
497  */
498 Date.prototype.getGMTOffset = function() {
499     return (this.getTimezoneOffset() > 0 ? "-" : "+")
500         + String.leftPad(Math.abs(Math.floor(this.getTimezoneOffset() / 60)), 2, "0")
501         + String.leftPad(this.getTimezoneOffset() % 60, 2, "0");
502 };
503
504 /**
505  * Get the offset from GMT of the current date (equivalent to the format specifier 'P').
506  * @return {String} 2-characters representing hours and 2-characters representing minutes
507  * seperated by a colon and prefixed with + or - (e.g. '-06:00')
508  */
509 Date.prototype.getGMTColonOffset = function() {
510         return (this.getTimezoneOffset() > 0 ? "-" : "+")
511                 + String.leftPad(Math.abs(Math.floor(this.getTimezoneOffset() / 60)), 2, "0")
512                 + ":"
513                 + String.leftPad(this.getTimezoneOffset() %60, 2, "0");
514 }
515
516 /**
517  * Get the numeric day number of the year, adjusted for leap year.
518  * @return {Number} 0 through 364 (365 in leap years)
519  */
520 Date.prototype.getDayOfYear = function() {
521     var num = 0;
522     Date.daysInMonth[1] = this.isLeapYear() ? 29 : 28;
523     for (var i = 0; i < this.getMonth(); ++i) {
524         num += Date.daysInMonth[i];
525     }
526     return num + this.getDate() - 1;
527 };
528
529 /**
530  * Get the string representation of the numeric week number of the year
531  * (equivalent to the format specifier 'W').
532  * @return {String} '00' through '52'
533  */
534 Date.prototype.getWeekOfYear = function() {
535     // Skip to Thursday of this week
536     var now = this.getDayOfYear() + (4 - this.getDay());
537     // Find the first Thursday of the year
538     var jan1 = new Date(this.getFullYear(), 0, 1);
539     var then = (7 - jan1.getDay() + 4);
540     return String.leftPad(((now - then) / 7) + 1, 2, "0");
541 };
542
543 /**
544  * Whether or not the current date is in a leap year.
545  * @return {Boolean} True if the current date is in a leap year, else false
546  */
547 Date.prototype.isLeapYear = function() {
548     var year = this.getFullYear();
549     return ((year & 3) == 0 && (year % 100 || (year % 400 == 0 && year)));
550 };
551
552 /**
553  * Get the first day of the current month, adjusted for leap year.  The returned value
554  * is the numeric day index within the week (0-6) which can be used in conjunction with
555  * the {@link #monthNames} array to retrieve the textual day name.
556  * Example:
557  *<pre><code>
558 var dt = new Date('1/10/2007');
559 document.write(Date.dayNames[dt.getFirstDayOfMonth()]); //output: 'Monday'
560 </code></pre>
561  * @return {Number} The day number (0-6)
562  */
563 Date.prototype.getFirstDayOfMonth = function() {
564     var day = (this.getDay() - (this.getDate() - 1)) % 7;
565     return (day < 0) ? (day + 7) : day;
566 };
567
568 /**
569  * Get the last day of the current month, adjusted for leap year.  The returned value
570  * is the numeric day index within the week (0-6) which can be used in conjunction with
571  * the {@link #monthNames} array to retrieve the textual day name.
572  * Example:
573  *<pre><code>
574 var dt = new Date('1/10/2007');
575 document.write(Date.dayNames[dt.getLastDayOfMonth()]); //output: 'Wednesday'
576 </code></pre>
577  * @return {Number} The day number (0-6)
578  */
579 Date.prototype.getLastDayOfMonth = function() {
580     var day = (this.getDay() + (Date.daysInMonth[this.getMonth()] - this.getDate())) % 7;
581     return (day < 0) ? (day + 7) : day;
582 };
583
584
585 /**
586  * Get the first date of this date's month
587  * @return {Date}
588  */
589 Date.prototype.getFirstDateOfMonth = function() {
590     return new Date(this.getFullYear(), this.getMonth(), 1);
591 };
592
593 /**
594  * Get the last date of this date's month
595  * @return {Date}
596  */
597 Date.prototype.getLastDateOfMonth = function() {
598     return new Date(this.getFullYear(), this.getMonth(), this.getDaysInMonth());
599 };
600 /**
601  * Get the number of days in the current month, adjusted for leap year.
602  * @return {Number} The number of days in the month
603  */
604 Date.prototype.getDaysInMonth = function() {
605     Date.daysInMonth[1] = this.isLeapYear() ? 29 : 28;
606     return Date.daysInMonth[this.getMonth()];
607 };
608
609 /**
610  * Get the English ordinal suffix of the current day (equivalent to the format specifier 'S').
611  * @return {String} 'st, 'nd', 'rd' or 'th'
612  */
613 Date.prototype.getSuffix = function() {
614     switch (this.getDate()) {
615         case 1:
616         case 21:
617         case 31:
618             return "st";
619         case 2:
620         case 22:
621             return "nd";
622         case 3:
623         case 23:
624             return "rd";
625         default:
626             return "th";
627     }
628 };
629
630 // private
631 Date.daysInMonth = [31,28,31,30,31,30,31,31,30,31,30,31];
632
633 /**
634  * An array of textual month names.
635  * Override these values for international dates, for example...
636  * Date.monthNames = ['JanInYourLang', 'FebInYourLang', ...];
637  * @type Array
638  * @static
639  */
640 Date.monthNames =
641    ["January",
642     "February",
643     "March",
644     "April",
645     "May",
646     "June",
647     "July",
648     "August",
649     "September",
650     "October",
651     "November",
652     "December"];
653
654 /**
655  * An array of textual day names.
656  * Override these values for international dates, for example...
657  * Date.dayNames = ['SundayInYourLang', 'MondayInYourLang', ...];
658  * @type Array
659  * @static
660  */
661 Date.dayNames =
662    ["Sunday",
663     "Monday",
664     "Tuesday",
665     "Wednesday",
666     "Thursday",
667     "Friday",
668     "Saturday"];
669
670 // private
671 Date.y2kYear = 50;
672 // private
673 Date.monthNumbers = {
674     Jan:0,
675     Feb:1,
676     Mar:2,
677     Apr:3,
678     May:4,
679     Jun:5,
680     Jul:6,
681     Aug:7,
682     Sep:8,
683     Oct:9,
684     Nov:10,
685     Dec:11};
686
687 /**
688  * Creates and returns a new Date instance with the exact same date value as the called instance.
689  * Dates are copied and passed by reference, so if a copied date variable is modified later, the original
690  * variable will also be changed.  When the intention is to create a new variable that will not
691  * modify the original instance, you should create a clone.
692  *
693  * Example of correctly cloning a date:
694  * <pre><code>
695 //wrong way:
696 var orig = new Date('10/1/2006');
697 var copy = orig;
698 copy.setDate(5);
699 document.write(orig);  //returns 'Thu Oct 05 2006'!
700
701 //correct way:
702 var orig = new Date('10/1/2006');
703 var copy = orig.clone();
704 copy.setDate(5);
705 document.write(orig);  //returns 'Thu Oct 01 2006'
706 </code></pre>
707  * @return {Date} The new Date instance
708  */
709 Date.prototype.clone = function() {
710         return new Date(this.getTime());
711 };
712
713 /**
714  * Clears any time information from this date
715  @param {Boolean} clone true to create a clone of this date, clear the time and return it
716  @return {Date} this or the clone
717  */
718 Date.prototype.clearTime = function(clone){
719     if(clone){
720         return this.clone().clearTime();
721     }
722     this.setHours(0);
723     this.setMinutes(0);
724     this.setSeconds(0);
725     this.setMilliseconds(0);
726     return this;
727 };
728
729 // private
730 // safari setMonth is broken -- check that this is only donw once...
731 if(Roo.isSafari && typeof(Date.brokenSetMonth) == 'undefined'){
732     Date.brokenSetMonth = Date.prototype.setMonth;
733         Date.prototype.setMonth = function(num){
734                 if(num <= -1){
735                         var n = Math.ceil(-num);
736                         var back_year = Math.ceil(n/12);
737                         var month = (n % 12) ? 12 - n % 12 : 0 ;
738                         this.setFullYear(this.getFullYear() - back_year);
739                         return Date.brokenSetMonth.call(this, month);
740                 } else {
741                         return Date.brokenSetMonth.apply(this, arguments);
742                 }
743         };
744 }
745
746 /** Date interval constant 
747 * @static 
748 * @type String */
749 Date.MILLI = "ms";
750 /** Date interval constant 
751 * @static 
752 * @type String */
753 Date.SECOND = "s";
754 /** Date interval constant 
755 * @static 
756 * @type String */
757 Date.MINUTE = "mi";
758 /** Date interval constant 
759 * @static 
760 * @type String */
761 Date.HOUR = "h";
762 /** Date interval constant 
763 * @static 
764 * @type String */
765 Date.DAY = "d";
766 /** Date interval constant 
767 * @static 
768 * @type String */
769 Date.MONTH = "mo";
770 /** Date interval constant 
771 * @static 
772 * @type String */
773 Date.YEAR = "y";
774
775 /**
776  * Provides a convenient method of performing basic date arithmetic.  This method
777  * does not modify the Date instance being called - it creates and returns
778  * a new Date instance containing the resulting date value.
779  *
780  * Examples:
781  * <pre><code>
782 //Basic usage:
783 var dt = new Date('10/29/2006').add(Date.DAY, 5);
784 document.write(dt); //returns 'Fri Oct 06 2006 00:00:00'
785
786 //Negative values will subtract correctly:
787 var dt2 = new Date('10/1/2006').add(Date.DAY, -5);
788 document.write(dt2); //returns 'Tue Sep 26 2006 00:00:00'
789
790 //You can even chain several calls together in one line!
791 var dt3 = new Date('10/1/2006').add(Date.DAY, 5).add(Date.HOUR, 8).add(Date.MINUTE, -30);
792 document.write(dt3); //returns 'Fri Oct 06 2006 07:30:00'
793  </code></pre>
794  *
795  * @param {String} interval   A valid date interval enum value
796  * @param {Number} value      The amount to add to the current date
797  * @return {Date} The new Date instance
798  */
799 Date.prototype.add = function(interval, value){
800   var d = this.clone();
801   if (!interval || value === 0) { return d; }
802   switch(interval.toLowerCase()){
803     case Date.MILLI:
804       d.setMilliseconds(this.getMilliseconds() + value);
805       break;
806     case Date.SECOND:
807       d.setSeconds(this.getSeconds() + value);
808       break;
809     case Date.MINUTE:
810       d.setMinutes(this.getMinutes() + value);
811       break;
812     case Date.HOUR:
813       d.setHours(this.getHours() + value);
814       break;
815     case Date.DAY:
816       d.setDate(this.getDate() + value);
817       break;
818     case Date.MONTH:
819       var day = this.getDate();
820       if(day > 28){
821           day = Math.min(day, this.getFirstDateOfMonth().add('mo', value).getLastDateOfMonth().getDate());
822       }
823       d.setDate(day);
824       d.setMonth(this.getMonth() + value);
825       break;
826     case Date.YEAR:
827       d.setFullYear(this.getFullYear() + value);
828       break;
829   }
830   return d;
831 };