1
// ===================================================================
2
// Author: Matt Kruse <matt@mattkruse.com>
3
// WWW: http://www.mattkruse.com/
5
// NOTICE: You may use this code for any purpose, commercial or
6
// private, without any further permission from the author. You may
7
// remove this notice from your final code if you wish, however it is
8
// appreciated by the author if at least my web site address is kept.
10
// You may *NOT* re-distribute this code in any way except through its
11
// use. That means, you can include it in your product, or your web
12
// site, or any other form where the code is actually being used. You
13
// may not put the plain javascript up on your site for download or
14
// include it in your javascript libraries for download.
15
// If you wish to share this code with others, please just point them
16
// to the URL instead.
17
// Please DO NOT link directly to my .js files from your site. Copy
18
// the files to your server and use them there. Thank you.
19
// ===================================================================
22
// ------------------------------------------------------------------
23
// May 17, 2003: Fixed bug in parseDate() for dates <1970
24
// March 11, 2003: Added parseDate() function
25
// March 11, 2003: Added "NNN" formatting option. Doesn't match up
26
// perfectly with SimpleDateFormat formats, but
27
// backwards-compatability was required.
29
// ------------------------------------------------------------------
30
// These functions use the same 'format' strings as the
31
// java.text.SimpleDateFormat class, with minor exceptions.
32
// The format string consists of the following abbreviations:
34
// Field | Full Form | Short Form
35
// -------------+--------------------+-----------------------
36
// Year | yyyy (4 digits) | yy (2 digits), y (2 or 4 digits)
37
// Month | MMM (name or abbr.)| MM (2 digits), M (1 or 2 digits)
39
// Day of Month | dd (2 digits) | d (1 or 2 digits)
40
// Day of Week | EE (name) | E (abbr)
41
// Hour (1-12) | hh (2 digits) | h (1 or 2 digits)
42
// Hour (0-23) | HH (2 digits) | H (1 or 2 digits)
43
// Hour (0-11) | KK (2 digits) | K (1 or 2 digits)
44
// Hour (1-24) | kk (2 digits) | k (1 or 2 digits)
45
// Minute | mm (2 digits) | m (1 or 2 digits)
46
// Second | ss (2 digits) | s (1 or 2 digits)
49
// NOTE THE DIFFERENCE BETWEEN MM and mm! Month=MM, not mm!
51
// "MMM d, y" matches: January 01, 2000
54
// "M/d/yy" matches: 01/20/00
56
// "MMM dd, yyyy hh:mm:ssa" matches: "January 01, 2000 12:30:45AM"
57
// ------------------------------------------------------------------
59
var MONTH_NAMES=new Array('January','February','March','April','May','June','July','August','September','October','November','December','Jan','Feb','Mar','Apr','May','Jun','Jul','Aug','Sep','Oct','Nov','Dec');
60
var DAY_NAMES=new Array('Sunday','Monday','Tuesday','Wednesday','Thursday','Friday','Saturday','Sun','Mon','Tue','Wed','Thu','Fri','Sat');
61
function LZ(x) {return(x<0||x>9?"":"0")+x}
63
// ------------------------------------------------------------------
64
// isDate ( date_string, format_string )
65
// Returns true if date string matches format of format string and
66
// is a valid date. Else returns false.
67
// It is recommended that you trim whitespace around the value before
68
// passing it to this function, as whitespace is NOT ignored!
69
// ------------------------------------------------------------------
70
function isDate(val,format) {
71
var date=getDateFromFormat(val,format);
72
if (date==0) { return false; }
76
// -------------------------------------------------------------------
77
// compareDates(date1,date1format,date2,date2format)
78
// Compare two date strings to see which is greater.
80
// 1 if date1 is greater than date2
81
// 0 if date2 is greater than date1 of if they are the same
82
// -1 if either of the dates is in an invalid format
83
// -------------------------------------------------------------------
84
function compareDates(date1,dateformat1,date2,dateformat2) {
85
var d1=getDateFromFormat(date1,dateformat1);
86
var d2=getDateFromFormat(date2,dateformat2);
96
// ------------------------------------------------------------------
97
// formatDate (date_object, format)
98
// Returns a date in the output format specified.
99
// The format string uses the same abbreviations as in getDateFromFormat()
100
// ------------------------------------------------------------------
101
function formatDate(date,format) {
107
var y=date.getYear()+"";
108
var M=date.getMonth()+1;
109
var d=date.getDate();
111
var H=date.getHours();
112
var m=date.getMinutes();
113
var s=date.getSeconds();
114
var yyyy,yy,MMM,MM,dd,hh,h,mm,ss,ampm,HH,H,KK,K,kk,k;
115
// Convert real date parts into formatted versions
116
var value=new Object();
117
if (y.length < 4) {y=""+(y-0+1900);}
120
value["yy"]=y.substring(2,4);
123
value["MMM"]=MONTH_NAMES[M-1];
124
value["NNN"]=MONTH_NAMES[M+11];
127
value["E"]=DAY_NAMES[E+7];
128
value["EE"]=DAY_NAMES[E];
131
if (H==0){value["h"]=12;}
132
else if (H>12){value["h"]=H-12;}
134
value["hh"]=LZ(value["h"]);
135
if (H>11){value["K"]=H-12;} else {value["K"]=H;}
137
value["KK"]=LZ(value["K"]);
138
value["kk"]=LZ(value["k"]);
139
if (H > 11) { value["a"]="PM"; }
140
else { value["a"]="AM"; }
145
while (i_format < format.length) {
146
c=format.charAt(i_format);
148
while ((format.charAt(i_format)==c) && (i_format < format.length)) {
149
token += format.charAt(i_format++);
151
if (value[token] != null) { result=result + value[token]; }
152
else { result=result + token; }
157
// ------------------------------------------------------------------
158
// Utility functions for parsing in getDateFromFormat()
159
// ------------------------------------------------------------------
160
function _isInteger(val) {
161
var digits="1234567890";
162
for (var i=0; i < val.length; i++) {
163
if (digits.indexOf(val.charAt(i))==-1) { return false; }
167
function _getInt(str,i,minlength,maxlength) {
168
for (var x=maxlength; x>=minlength; x--) {
169
var token=str.substring(i,i+x);
170
if (token.length < minlength) { return null; }
171
if (_isInteger(token)) { return token; }
176
// ------------------------------------------------------------------
177
// getDateFromFormat( date_string , format_string )
179
// This function takes a date string and a format string. It matches
180
// If the date string matches the format string, it returns the
181
// getTime() of the date. If it does not match, it returns 0.
182
// ------------------------------------------------------------------
183
function getDateFromFormat(val,format) {
193
var year=now.getYear();
194
var month=now.getMonth()+1;
197
while (i_format < format.length) {
198
// Get next token from format string
199
c=format.charAt(i_format);
201
while ((format.charAt(i_format)==c) && (i_format < format.length)) {
202
token += format.charAt(i_format++);
204
// Extract contents of value based on format token
205
if (token=="yyyy" || token=="yy" || token=="y") {
206
if (token=="yyyy") { x=4;y=4; }
207
if (token=="yy") { x=2;y=2; }
208
if (token=="y") { x=2;y=4; }
209
year=_getInt(val,i_val,x,y);
210
if (year==null) { return 0; }
211
i_val += year.length;
212
if (year.length==2) {
213
if (year > 70) { year=1900+(year-0); }
214
else { year=2000+(year-0); }
217
else if (token=="MMM"||token=="NNN"){
219
for (var i=0; i<MONTH_NAMES.length; i++) {
220
var month_name=MONTH_NAMES[i];
221
if (val.substring(i_val,i_val+month_name.length).toLowerCase()==month_name.toLowerCase()) {
222
if (token=="MMM"||(token=="NNN"&&i>11)) {
224
if (month>12) { month -= 12; }
225
i_val += month_name.length;
230
if ((month < 1)||(month>12)){return 0;}
232
else if (token=="EE"||token=="E"){
233
for (var i=0; i<DAY_NAMES.length; i++) {
234
var day_name=DAY_NAMES[i];
235
if (val.substring(i_val,i_val+day_name.length).toLowerCase()==day_name.toLowerCase()) {
236
i_val += day_name.length;
241
else if (token=="MM"||token=="M") {
242
month=_getInt(val,i_val,token.length,2);
243
if(month==null||(month<1)||(month>12)){return 0;}
244
i_val+=month.length;}
245
else if (token=="dd"||token=="d") {
246
date=_getInt(val,i_val,token.length,2);
247
if(date==null||(date<1)||(date>31)){return 0;}
250
if (val.substring(i_val,i_val+token.length)!=token) {return 0;}
251
else {i_val+=token.length;}
254
// If there are any trailing characters left in the value, it doesn't match
255
if (i_val != val.length) { return 0; }
256
// Is date valid for month?
258
// Check for leap year
259
if ( ( (year%4==0)&&(year%100 != 0) ) || (year%400==0) ) { // leap year
260
if (date > 29){ return 0; }
262
else { if (date > 28) { return 0; } }
264
if ((month==4)||(month==6)||(month==9)||(month==11)) {
265
if (date > 30) { return 0; }
267
// Correct hours value
268
var newdate=new Date(year,month-1,date);
269
return newdate.getTime();
272
// ------------------------------------------------------------------
273
// parseDate( date_string [, prefer_euro_format] )
275
// This function takes a date string and tries to match it to a
276
// number of possible date formats to get the value. It will try to
277
// match against the following international formats, in this order:
278
// y-M-d MMM d, y MMM d,y y-MMM-d d-MMM-y MMM d
279
// M/d/y M-d-y M.d.y MMM-d M/d M-d
280
// d/M/y d-M-y d.M.y d-MMM d/M d-M
281
// A second argument may be passed to instruct the method to search
282
// for formats like d/M/y (european format) before M/d/y (American).
283
// Returns a Date object or null if no patterns match.
284
// ------------------------------------------------------------------
285
function parseDate(val) {
286
var preferEuro=(arguments.length==2)?arguments[1]:false;
287
generalFormats=new Array('y-M-d','MMM d, y','MMM d,y','y-MMM-d','d-MMM-y','MMM d');
288
monthFirst=new Array('M/d/y','M-d-y','M.d.y','MMM-d','M/d','M-d');
289
dateFirst =new Array('d/M/y','d-M-y','d.M.y','d-MMM','d/M','d-M');
290
var checkList=new Array('generalFormats',preferEuro?'dateFirst':'monthFirst',preferEuro?'monthFirst':'dateFirst');
292
for (var i=0; i<checkList.length; i++) {
293
var l=window[checkList[i]];
294
for (var j=0; j<l.length; j++) {
295
d=getDateFromFormat(val,l[j]);
296
if (d!=0) { return new Date(d); }
b'\\ No newline at end of file'