1 Array.prototype.exists = function (x) {
2 for (var i = 0; i < this.length; i++) {
3 if (this[i] == x) return true;
8 Date.prototype.formatDate = function (input,time) {
10 // a PHP date like function, for formatting date strings
11 // See: http://www.php.net/date
13 // input : format string
14 // time : epoch time (seconds, and optional)
16 // if time is not passed, formatting is based on
17 // the current "this" date object's set time.
20 // a, A, B, d, D, F, g, G, h, H, i, j, l (lowercase L), L,
21 // m, M, n, O, r, s, S, t, U, w, W, y, Y, z
24 // I (capital i), T, Z
26 var switches = ["a", "A", "B", "d", "D", "F", "g", "G", "h", "H",
27 "i", "j", "l", "L", "m", "M", "n", "O", "r", "s",
28 "S", "t", "U", "w", "W", "y", "Y", "z"];
29 var daysLong = ["Sunday", "Monday", "Tuesday", "Wednesday",
30 "Thursday", "Friday", "Saturday"];
31 var daysShort = ["Sun", "Mon", "Tue", "Wed",
33 var monthsShort = ["Jan", "Feb", "Mar", "Apr",
34 "May", "Jun", "Jul", "Aug", "Sep",
36 var monthsLong = ["January", "February", "March", "April",
37 "May", "June", "July", "August", "September",
38 "October", "November", "December"];
39 var daysSuffix = ["st", "nd", "rd", "th", "th", "th", "th", // 1st - 7th
40 "th", "th", "th", "th", "th", "th", "th", // 8th - 14th
41 "th", "th", "th", "th", "th", "th", "st", // 15th - 21st
42 "nd", "rd", "th", "th", "th", "th", "th", // 22nd - 28th
43 "th", "th", "st"]; // 29th - 31st
46 // Lowercase Ante meridiem and Post meridiem
47 return self.getHours() > 11? "pm" : "am";
50 // Uppercase Ante meridiem and Post meridiem
51 return self.getHours() > 11? "PM" : "AM";
55 // Swatch internet time. code simply grabbed from ppk,
56 // since I was feeling lazy:
57 // http://www.xs4all.nl/~ppk/js/beat.html
58 var off = (self.getTimezoneOffset() + 60)*60;
59 var theSeconds = (self.getHours() * 3600) +
60 (self.getMinutes() * 60) +
61 self.getSeconds() + off;
62 var beat = Math.floor(theSeconds/86.4);
63 if (beat > 1000) beat -= 1000;
64 if (beat < 0) beat += 1000;
65 if ((""+beat).length == 1) beat = "00"+beat;
66 if ((""+beat).length == 2) beat = "0"+beat;
71 // Day of the month, 2 digits with leading zeros
72 return new String(self.getDate()).length == 1?
73 "0"+self.getDate() : self.getDate();
76 // A textual representation of a day, three letters
77 return daysShort[self.getDay()];
80 // A full textual representation of a month
81 return monthsLong[self.getMonth()];
84 // 12-hour format of an hour without leading zeros
85 return self.getHours() > 12? self.getHours()-12 : self.getHours();
88 // 24-hour format of an hour without leading zeros
89 return self.getHours();
92 // 12-hour format of an hour with leading zeros
93 if (self.getHours() > 12) {
94 var s = new String(self.getHours()-12);
96 "0"+ (self.getHours()-12) : self.getHours()-12;
98 var s = new String(self.getHours());
100 "0"+self.getHours() : self.getHours();
104 // 24-hour format of an hour with leading zeros
105 return new String(self.getHours()).length == 1?
106 "0"+self.getHours() : self.getHours();
109 // Minutes with leading zeros
110 return new String(self.getMinutes()).length == 1?
111 "0"+self.getMinutes() : self.getMinutes();
114 // Day of the month without leading zeros
115 return self.getDate();
118 // A full textual representation of the day of the week
119 return daysLong[self.getDay()];
122 // leap year or not. 1 if leap year, 0 if not.
123 // the logic should match iso's 8601 standard.
126 (y_ % 4 == 0 && y_ % 100 != 0) ||
127 (y_ % 4 == 0 && y_ % 100 == 0 && y_ % 400 == 0)
135 // Numeric representation of a month, with leading zeros
136 return self.getMonth() < 9?
137 "0"+(self.getMonth()+1) :
141 // A short textual representation of a month, three letters
142 return monthsShort[self.getMonth()];
145 // Numeric representation of a month, without leading zeros
146 return self.getMonth()+1;
149 // Difference to Greenwich time (GMT) in hours
150 var os = Math.abs(self.getTimezoneOffset());
151 var h = ""+Math.floor(os/60);
153 h.length == 1? h = "0"+h:1;
154 m.length == 1? m = "0"+m:1;
155 return self.getTimezoneOffset() < 0 ? "+"+h+m : "-"+h+m;
158 // RFC 822 formatted date
161 r = D() + ", " + j() + " " + M() + " " + Y() +
162 // 16 : 01 : 07 +0200
163 " " + H() + ":" + i() + ":" + s() + " " + O();
167 // English ordinal suffix for the day of the month, 2 characters
168 return daysSuffix[self.getDate()-1];
171 // Seconds, with leading zeros
172 return new String(self.getSeconds()).length == 1?
173 "0"+self.getSeconds() : self.getSeconds();
177 // thanks to Matt Bannon for some much needed code-fixes here!
178 var daysinmonths = [null,31,28,31,30,31,30,31,31,30,31,30,31];
179 if (L()==1 && n()==2) return 29; // leap day
180 return daysinmonths[n()];
183 // Seconds since the Unix Epoch (January 1 1970 00:00:00 GMT)
184 return Math.round(self.getTime()/1000);
187 // Weeknumber, as per ISO specification:
188 // http://www.cl.cam.ac.uk/~mgk25/iso-time.html
190 // if the day is three days before newyears eve,
191 // there's a chance it's "week 1" of next year.
192 // here we check for that.
193 var beforeNY = 364+L() - z();
195 var weekday = w()!=0?w()-1:6; // makes sunday (0), into 6.
196 if (beforeNY <= 2 && weekday <= 2-beforeNY) {
199 // similarly, if the day is within threedays of newyears
200 // there's a chance it belongs in the old year.
201 var ny = new Date("January 1 " + Y() + " 00:00:00");
202 var nyDay = ny.getDay()!=0?ny.getDay()-1:6;
206 (afterNY >= (6-nyDay))
208 // Since I'm not sure we can just always return 53,
209 // i call the function here again, using the last day
210 // of the previous year, as the date, and then just
212 var prevNY = new Date("December 31 " + (Y()-1) + " 00:00:00");
213 return prevNY.formatDate("W");
216 // week 1, is the week that has the first thursday in it.
217 // note that this value is not zero index.
219 // first day of the year fell on a thursday, or earlier.
220 return 1 + Math.floor( ( z() + nyDay ) / 7 );
222 // first day of the year fell on a friday, or later.
223 return 1 + Math.floor( ( z() - ( 7 - nyDay ) ) / 7 );
227 // Numeric representation of the day of the week
228 return self.getDay();
232 // A full numeric representation of a year, 4 digits
234 // we first check, if getFullYear is supported. if it
235 // is, we just use that. ppks code is nice, but wont
236 // work with dates outside 1900-2038, or something like that
237 if (self.getFullYear) {
238 var newDate = new Date("January 1 2001 00:00:00 +0000");
239 var x = newDate .getFullYear();
241 // i trust the method now
242 return self.getFullYear();
246 // codes thanks to ppk:
247 // http://www.xs4all.nl/~ppk/js/introdate.html
248 var x = self.getYear();
250 y += (y < 38) ? 2000 : 1900;
254 // A two-digit representation of a year
256 return y.substring(y.length-2,y.length);
259 // The day of the year, zero indexed! 0 through 366
260 var t = new Date("January 1 " + Y() + " 00:00:00");
261 var diff = self.getTime() - t.getTime();
262 return Math.floor(diff/1000/60/60/24);
268 var prevTime = self.getTime();
272 var ia = input.split("");
275 if (ia[ij] == "\\") {
276 // this is our way of allowing users to escape stuff
279 if (switches.exists(ia[ij])) {
280 ia[ij] = eval(ia[ij] + "()");
285 // reset time, back to what it was
287 self.setTime(prevTime);