3 <script src=
"../htmlrunner.js"></script>
5 function arrayExists(array
, x
) {
6 for (var i
= 0; i
< array
.length
; i
++) {
7 if (array
[i
] == x
) return true;
12 Date
.prototype.formatDate = function (input
,time
) {
14 // a PHP date like function, for formatting date strings
15 // See: http://www.php.net/date
17 // input : format string
18 // time : epoch time (seconds, and optional)
20 // if time is not passed, formatting is based on
21 // the current "this" date object's set time.
24 // a, A, B, d, D, F, g, G, h, H, i, j, l (lowercase L), L,
25 // m, M, n, O, r, s, S, t, U, w, W, y, Y, z
28 // I (capital i), T, Z
30 var switches
= ["a", "A", "B", "d", "D", "F", "g", "G", "h", "H",
31 "i", "j", "l", "L", "m", "M", "n", "O", "r", "s",
32 "S", "t", "U", "w", "W", "y", "Y", "z"];
33 var daysLong
= ["Sunday", "Monday", "Tuesday", "Wednesday",
34 "Thursday", "Friday", "Saturday"];
35 var daysShort
= ["Sun", "Mon", "Tue", "Wed",
37 var monthsShort
= ["Jan", "Feb", "Mar", "Apr",
38 "May", "Jun", "Jul", "Aug", "Sep",
40 var monthsLong
= ["January", "February", "March", "April",
41 "May", "June", "July", "August", "September",
42 "October", "November", "December"];
43 var daysSuffix
= ["st", "nd", "rd", "th", "th", "th", "th", // 1st - 7th
44 "th", "th", "th", "th", "th", "th", "th", // 8th - 14th
45 "th", "th", "th", "th", "th", "th", "st", // 15th - 21st
46 "nd", "rd", "th", "th", "th", "th", "th", // 22nd - 28th
47 "th", "th", "st"]; // 29th - 31st
50 // Lowercase Ante meridiem and Post meridiem
51 return self
.getHours() > 11? "pm" : "am";
54 // Uppercase Ante meridiem and Post meridiem
55 return self
.getHours() > 11? "PM" : "AM";
59 // Swatch internet time. code simply grabbed from ppk,
60 // since I was feeling lazy:
61 // http://www.xs4all.nl/~ppk/js/beat.html
62 var off
= (self
.getTimezoneOffset() + 60)*60;
63 var theSeconds
= (self
.getHours() * 3600) +
64 (self
.getMinutes() * 60) +
65 self
.getSeconds() + off
;
66 var beat
= Math
.floor(theSeconds
/86.4);
67 if (beat
> 1000) beat
-= 1000;
68 if (beat
< 0) beat
+= 1000;
69 if ((""+beat
).length
== 1) beat
= "00"+beat
;
70 if ((""+beat
).length
== 2) beat
= "0"+beat
;
75 // Day of the month, 2 digits with leading zeros
76 return new String(self
.getDate()).length
== 1?
77 "0"+self
.getDate() : self
.getDate();
80 // A textual representation of a day, three letters
81 return daysShort
[self
.getDay()];
84 // A full textual representation of a month
85 return monthsLong
[self
.getMonth()];
88 // 12-hour format of an hour without leading zeros
89 return self
.getHours() > 12? self
.getHours()-12 : self
.getHours();
92 // 24-hour format of an hour without leading zeros
93 return self
.getHours();
96 // 12-hour format of an hour with leading zeros
97 if (self
.getHours() > 12) {
98 var s
= new String(self
.getHours()-12);
100 "0"+ (self
.getHours()-12) : self
.getHours()-12;
102 var s
= new String(self
.getHours());
103 return s
.length
== 1?
104 "0"+self
.getHours() : self
.getHours();
108 // 24-hour format of an hour with leading zeros
109 return new String(self
.getHours()).length
== 1?
110 "0"+self
.getHours() : self
.getHours();
113 // Minutes with leading zeros
114 return new String(self
.getMinutes()).length
== 1?
115 "0"+self
.getMinutes() : self
.getMinutes();
118 // Day of the month without leading zeros
119 return self
.getDate();
122 // A full textual representation of the day of the week
123 return daysLong
[self
.getDay()];
126 // leap year or not. 1 if leap year, 0 if not.
127 // the logic should match iso's 8601 standard.
130 (y_
% 4 == 0 && y_
% 100 != 0) ||
131 (y_
% 4 == 0 && y_
% 100 == 0 && y_
% 400 == 0)
139 // Numeric representation of a month, with leading zeros
140 return self
.getMonth() < 9?
141 "0"+(self
.getMonth()+1) :
145 // A short textual representation of a month, three letters
146 return monthsShort
[self
.getMonth()];
149 // Numeric representation of a month, without leading zeros
150 return self
.getMonth()+1;
153 // Difference to Greenwich time (GMT) in hours
154 var os
= Math
.abs(self
.getTimezoneOffset());
155 var h
= ""+Math
.floor(os
/60);
157 h
.length
== 1? h
= "0"+h
:1;
158 m
.length
== 1? m
= "0"+m
:1;
159 return self
.getTimezoneOffset() < 0 ? "+"+h
+m
: "-"+h
+m
;
162 // RFC 822 formatted date
165 r
= D() + ", " + j() + " " + M() + " " + Y() +
166 // 16 : 01 : 07 +0200
167 " " + H() + ":" + i() + ":" + s() + " " + O();
171 // English ordinal suffix for the day of the month, 2 characters
172 return daysSuffix
[self
.getDate()-1];
175 // Seconds, with leading zeros
176 return new String(self
.getSeconds()).length
== 1?
177 "0"+self
.getSeconds() : self
.getSeconds();
181 // thanks to Matt Bannon for some much needed code-fixes here!
182 var daysinmonths
= [null,31,28,31,30,31,30,31,31,30,31,30,31];
183 if (L()==1 && n()==2) return 29; // leap day
184 return daysinmonths
[n()];
187 // Seconds since the Unix Epoch (January 1 1970 00:00:00 GMT)
188 return Math
.round(self
.getTime()/1000);
191 // Weeknumber, as per ISO specification:
192 // http://www.cl.cam.ac.uk/~mgk25/iso-time.html
194 // if the day is three days before newyears eve,
195 // there's a chance it's "week 1" of next year.
196 // here we check for that.
197 var beforeNY
= 364+L() - z();
199 var weekday
= w()!=0?w()-1:6; // makes sunday (0), into 6.
200 if (beforeNY
<= 2 && weekday
<= 2-beforeNY
) {
203 // similarly, if the day is within threedays of newyears
204 // there's a chance it belongs in the old year.
205 var ny
= new Date("January 1 " + Y() + " 00:00:00");
206 var nyDay
= ny
.getDay()!=0?ny
.getDay()-1:6;
210 (afterNY
>= (6-nyDay
))
212 // Since I'm not sure we can just always return 53,
213 // i call the function here again, using the last day
214 // of the previous year, as the date, and then just
216 var prevNY
= new Date("December 31 " + (Y()-1) + " 00:00:00");
217 return prevNY
.formatDate("W");
220 // week 1, is the week that has the first thursday in it.
221 // note that this value is not zero index.
223 // first day of the year fell on a thursday, or earlier.
224 return 1 + Math
.floor( ( z() + nyDay
) / 7 );
226 // first day of the year fell on a friday, or later.
227 return 1 + Math
.floor( ( z() - ( 7 - nyDay
) ) / 7 );
231 // Numeric representation of the day of the week
232 return self
.getDay();
236 // A full numeric representation of a year, 4 digits
238 // we first check, if getFullYear is supported. if it
239 // is, we just use that. ppks code is nice, but wont
240 // work with dates outside 1900-2038, or something like that
241 if (self
.getFullYear
) {
242 var newDate
= new Date("January 1 2001 00:00:00 +0000");
243 var x
= newDate
.getFullYear();
245 // i trust the method now
246 return self
.getFullYear();
250 // codes thanks to ppk:
251 // http://www.xs4all.nl/~ppk/js/introdate.html
252 var x
= self
.getYear();
254 y
+= (y
< 38) ? 2000 : 1900;
258 // A two-digit representation of a year
260 return y
.substring(y
.length
-2,y
.length
);
263 // The day of the year, zero indexed! 0 through 366
264 var t
= new Date("January 1 " + Y() + " 00:00:00");
265 var diff
= self
.getTime() - t
.getTime();
266 return Math
.floor(diff
/1000/60/60/24);
272 var prevTime
= self
.getTime();
276 var ia
= input
.split("");
279 if (ia
[ij
] == "\\") {
280 // this is our way of allowing users to escape stuff
283 if (arrayExists(switches
,ia
[ij
])) {
284 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");
298 window
.onload = function(){ startTest("sunspider-date-format-tofte", '393fb742');
300 test("Format Date", function(){
301 for (var i
= 0; i
< 50; ++i
) {
302 var shortFormat
= date
.formatDate("Y-m-d");
303 var longFormat
= date
.formatDate("l, F d, Y g:i:s A");
304 date
.setTime(date
.getTime() + 84266956);