2 * A Javascript date chooser.
3 * Copyright (C) 2004 Baron Schwartz <baron at sequent dot org>
5 * This program is free software; you can redistribute it and/or modify it under
6 * the terms of the GNU General Public License as published by the Free Software
7 * Foundation; either version 2 of the License, or (at your option) any later
10 * This program is distributed in the hope that it will be useful, but WITHOUT
11 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
12 * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
15 * You should have received a copy of the GNU General Public License along with
16 * this program; if not, write to the Free Software Foundation, Inc., 59 Temple
17 * Place, Suite 330, Boston, MA 02111-1307 USA
20 Date.formatFunctions = {count:0};
22 Date.prototype.dateFormat = function(format) {
23 if (Date.formatFunctions[format] == null) {
24 Date.createNewFormat(format);
26 var func = Date.formatFunctions[format];
27 return eval("this." + func + "();");
30 Date.createNewFormat = function(format) {
31 var funcName = "format" + Date.formatFunctions.count++;
32 Date.formatFunctions[format] = funcName;
33 var code = "Date.prototype." + funcName + " = function(){return ";
36 for (var i = 0; i < format.length; ++i) {
37 char = format.charAt(i);
38 if (!special && char == "\\") {
43 code += "'" + String.escape(char) + "' + ";
46 code += Date.getFormatCode(char);
49 eval(code.substring(0, code.length - 3) + ";}");
52 Date.getFormatCode = function(character) {
55 return "String.leftPad(this.getDate(), 2, '0') + ";
57 return "Date.dayNames[this.getDay()].substring(0, 3) + ";
59 return "this.getDate() + ";
61 return "Date.dayNames[this.getDay()] + ";
63 return "this.getSuffix() + ";
65 return "this.getDay() + ";
67 return "this.getDayOfYear() + ";
69 return "this.getWeekOfYear() + ";
71 return "Date.monthNames[this.getMonth()] + ";
73 return "String.leftPad(this.getMonth() + 1, 2, '0') + ";
75 return "Date.monthNames[this.getMonth()].substring(0, 3) + ";
77 return "(this.getMonth() + 1) + ";
79 return "this.getDaysInMonth() + ";
81 return "(this.isLeapYear() ? 1 : 0) + ";
83 return "this.getFullYear() + ";
85 return "('' + this.getFullYear()).substring(2, 4) + ";
87 return "(this.getHours() < 12 ? 'am' : 'pm') + ";
89 return "(this.getHours() < 12 ? 'AM' : 'PM') + ";
91 return "((this.getHours() %12) ? this.getHours() % 12 : 12) + ";
93 return "this.getHours() + ";
95 return "String.leftPad((this.getHours() %12) ? this.getHours() % 12 : 12, 2, '0') + ";
97 return "String.leftPad(this.getHours(), 2, '0') + ";
99 return "String.leftPad(this.getMinutes(), 2, '0') + ";
101 return "String.leftPad(this.getSeconds(), 2, '0') + ";
103 return "this.getGMTOffset() + ";
105 return "this.getTimezone() + ";
107 return "(this.getTimezoneOffset() * -60) + ";
109 return "'" + String.escape(character) + "' + ";
113 Date.prototype.getTimezone = function() {
114 return this.toString().replace(
115 /^.*? ([A-Z]{3}) [0-9]{4}.*$/, "$1").replace(
116 /^.*?\(([A-Z])[a-z]+ ([A-Z])[a-z]+ ([A-Z])[a-z]+\)$/, "$1$2$3");
119 Date.prototype.getGMTOffset = function() {
120 return (this.getTimezoneOffset() > 0 ? "-" : "+")
121 + String.leftPad(Math.floor(this.getTimezoneOffset() / 60), 2, "0")
122 + String.leftPad(this.getTimezoneOffset() % 60, 2, "0");
125 Date.prototype.getDayOfYear = function() {
127 Date.daysInMonth[1] = this.isLeapYear() ? 29 : 28;
128 for (var i = 0; i < this.getMonth(); ++i) {
129 num += Date.daysInMonth[i];
131 return num + this.getDate() - 1;
134 Date.prototype.getWeekOfYear = function() {
135 // Skip to Thursday of this week
136 var now = this.getDayOfYear() + (4 - this.getDay());
137 // Find the first Thursday of the year
138 var jan1 = new Date(this.getFullYear(), 0, 1);
139 var then = (7 - jan1.getDay() + 4);
140 document.write(then);
141 return String.leftPad(((now - then) / 7) + 1, 2, "0");
144 Date.prototype.isLeapYear = function() {
145 var year = this.getFullYear();
146 return ((year & 3) == 0 && (year % 100 || (year % 400 == 0 && year)));
149 Date.prototype.getFirstDayOfMonth = function() {
150 var day = (this.getDay() - (this.getDate() - 1)) % 7;
151 return (day < 0) ? (day + 7) : day;
154 Date.prototype.getLastDayOfMonth = function() {
155 var day = (this.getDay() + (Date.daysInMonth[this.getMonth()] - this.getDate())) % 7;
156 return (day < 0) ? (day + 7) : day;
159 Date.prototype.getDaysInMonth = function() {
160 Date.daysInMonth[1] = this.isLeapYear() ? 29 : 28;
161 return Date.daysInMonth[this.getMonth()];
164 Date.prototype.getSuffix = function() {
165 switch (this.getDate()) {
181 String.escape = function(string) {
182 return string.replace(/('|\\)/g, "\\$1");
185 String.leftPad = function (val, size, char) {
186 var result = new String(val);
190 while (result.length < size) {
191 result = char + result;
196 Date.daysInMonth = [31,28,31,30,31,30,31,31,30,31,30,31];
219 ISO8601LongPattern:"Y-m-d H:i:s",
220 ISO8601ShortPattern:"Y-m-d",
221 ShortDatePattern: "n/j/Y",
222 LongDatePattern: "l, F d, Y",
223 FullDateTimePattern: "l, F d, Y g:i:s A",
224 MonthDayPattern: "F d",
225 ShortTimePattern: "g:i A",
226 LongTimePattern: "g:i:s A",
227 SortableDateTimePattern: "Y-m-d\\TH:i:s",
228 UniversalSortableDateTimePattern: "Y-m-d H:i:sO",
229 YearMonthPattern: "F, Y"};
231 Date.parseIsoDate = function(dateString) {
232 var result = new Date();
233 var re = /^(\d\d\d\d)-(\d\d)-(\d\d)( (\d\d):(\d\d):(\d\d))?$/;
234 var match = dateString.match(re);
236 if (match[5] != null && match[5] != '') {
238 parseInt(match[1], 10),
239 parseInt(match[2], 10) - 1,
240 parseInt(match[3], 10),
241 parseInt(match[5], 10),
242 parseInt(match[6], 10),
243 parseInt(match[7], 10));
247 parseInt(match[1], 10),
248 parseInt(match[2], 10) - 1,
249 parseInt(match[3], 10));