2 * Copyright (C) 2004 Baron Schwartz <baron at sequent dot org>
4 * This program is free software; you can redistribute it and/or modify it
5 * under the terms of the GNU Lesser General Public License as published by the
6 * Free Software Foundation, version 2.1.
8 * This program is distributed in the hope that it will be useful, but WITHOUT
9 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
10 * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
14 Date.parseFunctions = {count:0};
15 Date.parseRegexes = [];
16 Date.formatFunctions = {count:0};
18 Date.prototype.dateFormat = function(format) {
19 if (Date.formatFunctions[format] == null) {
20 Date.createNewFormat(format);
22 var func = Date.formatFunctions[format];
26 Date.createNewFormat = function(format) {
27 var funcName = "format" + Date.formatFunctions.count++;
28 Date.formatFunctions[format] = funcName;
29 var code = "Date.prototype." + funcName + " = function(){return ";
32 for (var i = 0; i < format.length; ++i) {
33 ch = format.charAt(i);
34 if (!special && ch == "\\") {
39 code += "'" + String.escape(ch) + "' + ";
42 code += Date.getFormatCode(ch);
45 eval(code.substring(0, code.length - 3) + ";}");
48 Date.getFormatCode = function(character) {
51 return "String.leftPad(this.getDate(), 2, '0') + ";
53 return "Date.dayNames[this.getDay()].substring(0, 3) + ";
55 return "this.getDate() + ";
57 return "Date.dayNames[this.getDay()] + ";
59 return "this.getSuffix() + ";
61 return "this.getDay() + ";
63 return "this.getDayOfYear() + ";
65 return "this.getWeekOfYear() + ";
67 return "Date.monthNames[this.getMonth()] + ";
69 return "String.leftPad(this.getMonth() + 1, 2, '0') + ";
71 return "Date.monthNames[this.getMonth()].substring(0, 3) + ";
73 return "(this.getMonth() + 1) + ";
75 return "this.getDaysInMonth() + ";
77 return "(this.isLeapYear() ? 1 : 0) + ";
79 return "this.getFullYear() + ";
81 return "('' + this.getFullYear()).substring(2, 4) + ";
83 return "(this.getHours() < 12 ? 'am' : 'pm') + ";
85 return "(this.getHours() < 12 ? 'AM' : 'PM') + ";
87 return "((this.getHours() %12) ? this.getHours() % 12 : 12) + ";
89 return "this.getHours() + ";
91 return "String.leftPad((this.getHours() %12) ? this.getHours() % 12 : 12, 2, '0') + ";
93 return "String.leftPad(this.getHours(), 2, '0') + ";
95 return "String.leftPad(this.getMinutes(), 2, '0') + ";
97 return "String.leftPad(this.getSeconds(), 2, '0') + ";
99 return "this.getGMTOffset() + ";
101 return "this.getTimezone() + ";
103 return "(this.getTimezoneOffset() * -60) + ";
105 return "'" + String.escape(character) + "' + ";
109 Date.parseDate = function(input, format) {
110 if (Date.parseFunctions[format] == null) {
111 Date.createParser(format);
113 var func = Date.parseFunctions[format];
114 return Date[func](input);
117 Date.createParser = function(format) {
118 var funcName = "parse" + Date.parseFunctions.count++;
119 var regexNum = Date.parseRegexes.length;
120 var currentGroup = 1;
121 Date.parseFunctions[format] = funcName;
123 var code = "Date." + funcName + " = function(input){\n"
124 + "var y = -1, m = -1, d = -1, h = -1, i = -1, s = -1;\n"
125 + "var d = new Date();\n"
126 + "y = d.getFullYear();\n"
127 + "m = d.getMonth();\n"
128 + "d = d.getDate();\n"
129 + "var results = input.match(Date.parseRegexes[" + regexNum + "]);\n"
130 + "if (results && results.length > 0) {"
135 for (var i = 0; i < format.length; ++i) {
136 ch = format.charAt(i);
137 if (!special && ch == "\\") {
142 regex += String.escape(ch);
145 obj = Date.formatCodeToRegex(ch, currentGroup);
146 currentGroup += obj.g;
148 if (obj.g && obj.c) {
154 code += "if (y > 0 && m >= 0 && d > 0 && h >= 0 && i >= 0 && s >= 0)\n"
155 + "{return new Date(y, m, d, h, i, s);}\n"
156 + "else if (y > 0 && m >= 0 && d > 0 && h >= 0 && i >= 0)\n"
157 + "{return new Date(y, m, d, h, i);}\n"
158 + "else if (y > 0 && m >= 0 && d > 0 && h >= 0)\n"
159 + "{return new Date(y, m, d, h);}\n"
160 + "else if (y > 0 && m >= 0 && d > 0)\n"
161 + "{return new Date(y, m, d);}\n"
162 + "else if (y > 0 && m >= 0)\n"
163 + "{return new Date(y, m);}\n"
164 + "else if (y > 0)\n"
165 + "{return new Date(y);}\n"
168 Date.parseRegexes[regexNum] = new RegExp("^" + regex + "$");
172 Date.formatCodeToRegex = function(character, currentGroup) {
177 s:"(?:Sun|Mon|Tue|Wed|Thu|Fri|Sat)"};
181 c:"d = parseInt(results[" + currentGroup + "], 10);\n",
186 s:"(?:" + Date.dayNames.join("|") + ")"};
190 s:"(?:st|nd|rd|th)"};
205 c:"m = parseInt(Date.monthNumbers[results[" + currentGroup + "].substring(0, 3)], 10);\n",
206 s:"(" + Date.monthNames.join("|") + ")"};
209 c:"m = parseInt(Date.monthNumbers[results[" + currentGroup + "]], 10);\n",
210 s:"(Jan|Feb|Mar|Apr|May|Jun|Jul|Aug|Sep|Oct|Nov|Dec)"};
214 c:"m = parseInt(results[" + currentGroup + "], 10) - 1;\n",
226 c:"y = parseInt(results[" + currentGroup + "], 10);\n",
230 c:"var ty = parseInt(results[" + currentGroup + "], 10);\n"
231 + "y = ty > Date.y2kYear ? 1900 + ty : 2000 + ty;\n",
235 c:"if (results[" + currentGroup + "] == 'am') {\n"
236 + "if (h == 12) { h = 0; }\n"
237 + "} else { if (h < 12) { h += 12; }}",
241 c:"if (results[" + currentGroup + "] == 'AM') {\n"
242 + "if (h == 12) { h = 0; }\n"
243 + "} else { if (h < 12) { h += 12; }}",
250 c:"h = parseInt(results[" + currentGroup + "], 10);\n",
254 c:"i = parseInt(results[" + currentGroup + "], 10);\n",
258 c:"s = parseInt(results[" + currentGroup + "], 10);\n",
275 s:String.escape(character)};
279 Date.prototype.getTimezone = function() {
280 return this.toString().replace(
281 /^.*? ([A-Z]{3}) [0-9]{4}.*$/, "$1").replace(
282 /^.*?\(([A-Z])[a-z]+ ([A-Z])[a-z]+ ([A-Z])[a-z]+\)$/, "$1$2$3");
285 Date.prototype.getGMTOffset = function() {
286 return (this.getTimezoneOffset() > 0 ? "-" : "+")
287 + String.leftPad(Math.floor(this.getTimezoneOffset() / 60), 2, "0")
288 + String.leftPad(this.getTimezoneOffset() % 60, 2, "0");
291 Date.prototype.getDayOfYear = function() {
293 Date.daysInMonth[1] = this.isLeapYear() ? 29 : 28;
294 for (var i = 0; i < this.getMonth(); ++i) {
295 num += Date.daysInMonth[i];
297 return num + this.getDate() - 1;
300 Date.prototype.getWeekOfYear = function() {
301 // Skip to Thursday of this week
302 var now = this.getDayOfYear() + (4 - this.getDay());
303 // Find the first Thursday of the year
304 var jan1 = new Date(this.getFullYear(), 0, 1);
305 var then = (7 - jan1.getDay() + 4);
306 document.write(then);
307 return String.leftPad(((now - then) / 7) + 1, 2, "0");
310 Date.prototype.isLeapYear = function() {
311 var year = this.getFullYear();
312 return ((year & 3) == 0 && (year % 100 || (year % 400 == 0 && year)));
315 Date.prototype.getFirstDayOfMonth = function() {
316 var day = (this.getDay() - (this.getDate() - 1)) % 7;
317 return (day < 0) ? (day + 7) : day;
320 Date.prototype.getLastDayOfMonth = function() {
321 var day = (this.getDay() + (Date.daysInMonth[this.getMonth()] - this.getDate())) % 7;
322 return (day < 0) ? (day + 7) : day;
325 Date.prototype.getDaysInMonth = function() {
326 Date.daysInMonth[1] = this.isLeapYear() ? 29 : 28;
327 return Date.daysInMonth[this.getMonth()];
330 Date.prototype.getSuffix = function() {
331 switch (this.getDate()) {
347 String.escape = function(string) {
348 return string.replace(/('|\\)/g, "\\$1");
351 String.leftPad = function (val, size, ch) {
352 var result = new String(val);
356 while (result.length < size) {
357 result = ch + result;
362 Date.daysInMonth = [31,28,31,30,31,30,31,31,30,31,30,31];
385 Date.monthNumbers = {
399 ISO8601LongPattern:"Y-m-d H:i:s",
400 ISO8601ShortPattern:"Y-m-d",
401 ShortDatePattern: "n/j/Y",
402 LongDatePattern: "l, F d, Y",
403 FullDateTimePattern: "l, F d, Y g:i:s A",
404 MonthDayPattern: "F d",
405 ShortTimePattern: "g:i A",
406 LongTimePattern: "g:i:s A",
407 SortableDateTimePattern: "Y-m-d\\TH:i:s",
408 UniversalSortableDateTimePattern: "Y-m-d H:i:sO",
409 YearMonthPattern: "F, Y"};
411 var date = new Date("1/1/2007 1:11:11");
413 for (i = 0; i < 4000; ++i) {
414 var short = date.dateFormat("Y-m-d");
415 var long = date.dateFormat("l, F d, Y g:i:s A");
416 date.setTime(date.getTime() + 84266956);