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