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