1 function arrayExists(array, x) {
3 for (var i = 0; i < array.length; i++) {
4 if (array[i] == x) return true;
10 Date.prototype.formatDate = function (input,time) {
12 // a PHP date like function, for formatting date strings
13 // See: http://www.php.net/date
15 // input : format string
16 // time : epoch time (seconds, and optional)
18 // if time is not passed, formatting is based on
19 // the current "this" date object's set time.
22 // a, A, B, d, D, F, g, G, h, H, i, j, l (lowercase L), L,
23 // m, M, n, O, r, s, S, t, U, w, W, y, Y, z
26 // I (capital i), T, Z
28 var switches = ["a", "A", "B", "d", "D", "F", "g", "G", "h", "H",
29 "i", "j", "l", "L", "m", "M", "n", "O", "r", "s",
30 "S", "t", "U", "w", "W", "y", "Y", "z"];
31 var daysLong = ["Sunday", "Monday", "Tuesday", "Wednesday",
32 "Thursday", "Friday", "Saturday"];
33 var daysShort = ["Sun", "Mon", "Tue", "Wed",
35 var monthsShort = ["Jan", "Feb", "Mar", "Apr",
36 "May", "Jun", "Jul", "Aug", "Sep",
38 var monthsLong = ["January", "February", "March", "April",
39 "May", "June", "July", "August", "September",
40 "October", "November", "December"];
41 var daysSuffix = ["st", "nd", "rd", "th", "th", "th", "th", // 1st - 7th
42 "th", "th", "th", "th", "th", "th", "th", // 8th - 14th
43 "th", "th", "th", "th", "th", "th", "st", // 15th - 21st
44 "nd", "rd", "th", "th", "th", "th", "th", // 22nd - 28th
45 "th", "th", "st"]; // 29th - 31st
48 // Lowercase Ante meridiem and Post meridiem
49 return self.getHours() > 11? "pm" : "am";
52 // Uppercase Ante meridiem and Post meridiem
53 return self.getHours() > 11? "PM" : "AM";
57 // Swatch internet time. code simply grabbed from ppk,
58 // since I was feeling lazy:
59 // http://www.xs4all.nl/~ppk/js/beat.html
60 var off = (self.getTimezoneOffset() + 60)*60;
61 var theSeconds = (self.getHours() * 3600) +
62 (self.getMinutes() * 60) +
63 self.getSeconds() + off;
64 var beat = Math.floor(theSeconds/86.4);
65 if (beat > 1000) beat -= 1000;
66 if (beat < 0) beat += 1000;
67 if ((""+beat).length == 1) beat = "00"+beat;
68 if ((""+beat).length == 2) beat = "0"+beat;
73 // Day of the month, 2 digits with leading zeros
74 return new String(self.getDate()).length == 1?
75 "0"+self.getDate() : self.getDate();
78 // A textual representation of a day, three letters
79 return daysShort[self.getDay()];
82 // A full textual representation of a month
83 return monthsLong[self.getMonth()];
86 // 12-hour format of an hour without leading zeros
87 return self.getHours() > 12? self.getHours()-12 : self.getHours();
90 // 24-hour format of an hour without leading zeros
91 return self.getHours();
94 // 12-hour format of an hour with leading zeros
95 if (self.getHours() > 12) {
96 var s = new String(self.getHours()-12);
98 "0"+ (self.getHours()-12) : self.getHours()-12;
100 var s = new String(self.getHours());
101 return s.length == 1?
102 "0"+self.getHours() : self.getHours();
106 // 24-hour format of an hour with leading zeros
107 return new String(self.getHours()).length == 1?
108 "0"+self.getHours() : self.getHours();
111 // Minutes with leading zeros
112 return new String(self.getMinutes()).length == 1?
113 "0"+self.getMinutes() : self.getMinutes();
116 // Day of the month without leading zeros
117 return self.getDate();
120 // A full textual representation of the day of the week
121 return daysLong[self.getDay()];
124 // leap year or not. 1 if leap year, 0 if not.
125 // the logic should match iso's 8601 standard.
128 (y_ % 4 == 0 && y_ % 100 != 0) ||
129 (y_ % 4 == 0 && y_ % 100 == 0 && y_ % 400 == 0)
137 // Numeric representation of a month, with leading zeros
138 return self.getMonth() < 9?
139 "0"+(self.getMonth()+1) :
143 // A short textual representation of a month, three letters
144 return monthsShort[self.getMonth()];
147 // Numeric representation of a month, without leading zeros
148 return self.getMonth()+1;
151 // Difference to Greenwich time (GMT) in hours
152 var os = Math.abs(self.getTimezoneOffset());
153 var h = ""+Math.floor(os/60);
155 h.length == 1? h = "0"+h:1;
156 m.length == 1? m = "0"+m:1;
157 return self.getTimezoneOffset() < 0 ? "+"+h+m : "-"+h+m;
160 // RFC 822 formatted date
163 r = D() + ", " + j() + " " + M() + " " + Y() +
164 // 16 : 01 : 07 +0200
165 " " + H() + ":" + i() + ":" + s() + " " + O();
169 // English ordinal suffix for the day of the month, 2 characters
170 return daysSuffix[self.getDate()-1];
173 // Seconds, with leading zeros
174 return new String(self.getSeconds()).length == 1?
175 "0"+self.getSeconds() : self.getSeconds();
179 // thanks to Matt Bannon for some much needed code-fixes here!
180 var daysinmonths = [null,31,28,31,30,31,30,31,31,30,31,30,31];
181 if (L()==1 && n()==2) return 29; // leap day
182 return daysinmonths[n()];
185 // Seconds since the Unix Epoch (January 1 1970 00:00:00 GMT)
186 return Math.round(self.getTime()/1000);
189 // Weeknumber, as per ISO specification:
190 // http://www.cl.cam.ac.uk/~mgk25/iso-time.html
192 // if the day is three days before newyears eve,
193 // there's a chance it's "week 1" of next year.
194 // here we check for that.
195 var beforeNY = 364+L() - z();
197 var weekday = w()!=0?w()-1:6; // makes sunday (0), into 6.
198 if (beforeNY <= 2 && weekday <= 2-beforeNY) {
201 // similarly, if the day is within threedays of newyears
202 // there's a chance it belongs in the old year.
203 var ny = new Date("January 1 " + Y() + " 00:00:00");
204 var nyDay = ny.getDay()!=0?ny.getDay()-1:6;
208 (afterNY >= (6-nyDay))
210 // Since I'm not sure we can just always return 53,
211 // i call the function here again, using the last day
212 // of the previous year, as the date, and then just
214 var prevNY = new Date("December 31 " + (Y()-1) + " 00:00:00");
215 return prevNY.formatDate("W");
218 // week 1, is the week that has the first thursday in it.
219 // note that this value is not zero index.
221 // first day of the year fell on a thursday, or earlier.
222 return 1 + Math.floor( ( z() + nyDay ) / 7 );
224 // first day of the year fell on a friday, or later.
225 return 1 + Math.floor( ( z() - ( 7 - nyDay ) ) / 7 );
229 // Numeric representation of the day of the week
230 return self.getDay();
234 // A full numeric representation of a year, 4 digits
236 // we first check, if getFullYear is supported. if it
237 // is, we just use that. ppks code is nice, but wont
238 // work with dates outside 1900-2038, or something like that
239 if (self.getFullYear) {
240 var newDate = new Date("January 1 2001 00:00:00 +0000");
241 var x = newDate .getFullYear();
243 // i trust the method now
244 return self.getFullYear();
248 // codes thanks to ppk:
249 // http://www.xs4all.nl/~ppk/js/introdate.html
250 var x = self.getYear();
252 y += (y < 38) ? 2000 : 1900;
256 // A two-digit representation of a year
258 return y.substring(y.length-2,y.length);
261 // The day of the year, zero indexed! 0 through 366
262 var t = new Date("January 1 " + Y() + " 00:00:00");
263 var diff = self.getTime() - t.getTime();
264 return Math.floor(diff/1000/60/60/24);
270 var prevTime = self.getTime();
274 var ia = input.split("");
278 if (ia[ij] == "\\") {
279 // this is our way of allowing users to escape stuff
282 if (arrayExists(switches,ia[ij])) {
283 ia[ij] = eval(ia[ij] + "()");
289 // reset time, back to what it was
291 self.setTime(prevTime);
296 var date = new Date("1/1/2007 1:11:11");
299 for (i = 0; i < 500; ++i) {
300 var shortFormat = date.formatDate("Y-m-d");
301 var longFormat = date.formatDate("l, F d, Y g:i:s A");
302 date.setTime(date.getTime() + 84266956);