bgneal@1: /* 'Magic' date parsing, by Simon Willison (6th October 2003) bgneal@1: http://simon.incutio.com/archive/2003/10/06/betterDateInput bgneal@1: Adapted for 6newslawrence.com, 28th January 2004 bgneal@1: */ bgneal@1: bgneal@1: /* Finds the index of the first occurence of item in the array, or -1 if not found */ bgneal@1: if (typeof Array.prototype.indexOf == 'undefined') { bgneal@1: Array.prototype.indexOf = function(item) { bgneal@1: var len = this.length; bgneal@1: for (var i = 0; i < len; i++) { bgneal@1: if (this[i] == item) { bgneal@1: return i; bgneal@1: } bgneal@1: } bgneal@1: return -1; bgneal@1: }; bgneal@1: } bgneal@1: /* Returns an array of items judged 'true' by the passed in test function */ bgneal@1: if (typeof Array.prototype.filter == 'undefined') { bgneal@1: Array.prototype.filter = function(test) { bgneal@1: var matches = []; bgneal@1: var len = this.length; bgneal@1: for (var i = 0; i < len; i++) { bgneal@1: if (test(this[i])) { bgneal@1: matches[matches.length] = this[i]; bgneal@1: } bgneal@1: } bgneal@1: return matches; bgneal@1: }; bgneal@1: } bgneal@1: bgneal@1: var monthNames = gettext("January February March April May June July August September October November December").split(" "); bgneal@1: var weekdayNames = gettext("Sunday Monday Tuesday Wednesday Thursday Friday Saturday").split(" "); bgneal@1: bgneal@1: /* Takes a string, returns the index of the month matching that string, throws bgneal@1: an error if 0 or more than 1 matches bgneal@1: */ bgneal@1: function parseMonth(month) { bgneal@1: var matches = monthNames.filter(function(item) { bgneal@1: return new RegExp("^" + month, "i").test(item); bgneal@1: }); bgneal@1: if (matches.length == 0) { bgneal@1: throw new Error("Invalid month string"); bgneal@1: } bgneal@1: if (matches.length > 1) { bgneal@1: throw new Error("Ambiguous month"); bgneal@1: } bgneal@1: return monthNames.indexOf(matches[0]); bgneal@1: } bgneal@1: /* Same as parseMonth but for days of the week */ bgneal@1: function parseWeekday(weekday) { bgneal@1: var matches = weekdayNames.filter(function(item) { bgneal@1: return new RegExp("^" + weekday, "i").test(item); bgneal@1: }); bgneal@1: if (matches.length == 0) { bgneal@1: throw new Error("Invalid day string"); bgneal@1: } bgneal@1: if (matches.length > 1) { bgneal@1: throw new Error("Ambiguous weekday"); bgneal@1: } bgneal@1: return weekdayNames.indexOf(matches[0]); bgneal@1: } bgneal@1: bgneal@1: /* Array of objects, each has 're', a regular expression and 'handler', a bgneal@1: function for creating a date from something that matches the regular bgneal@1: expression. Handlers may throw errors if string is unparseable. bgneal@1: */ bgneal@1: var dateParsePatterns = [ bgneal@1: // Today bgneal@1: { re: /^tod/i, bgneal@1: handler: function() { bgneal@1: return new Date(); bgneal@1: } bgneal@1: }, bgneal@1: // Tomorrow bgneal@1: { re: /^tom/i, bgneal@1: handler: function() { bgneal@1: var d = new Date(); bgneal@1: d.setDate(d.getDate() + 1); bgneal@1: return d; bgneal@1: } bgneal@1: }, bgneal@1: // Yesterday bgneal@1: { re: /^yes/i, bgneal@1: handler: function() { bgneal@1: var d = new Date(); bgneal@1: d.setDate(d.getDate() - 1); bgneal@1: return d; bgneal@1: } bgneal@1: }, bgneal@1: // 4th bgneal@1: { re: /^(\d{1,2})(st|nd|rd|th)?$/i, bgneal@1: handler: function(bits) { bgneal@1: var d = new Date(); bgneal@1: d.setDate(parseInt(bits[1], 10)); bgneal@1: return d; bgneal@1: } bgneal@1: }, bgneal@1: // 4th Jan bgneal@1: { re: /^(\d{1,2})(?:st|nd|rd|th)? (\w+)$/i, bgneal@1: handler: function(bits) { bgneal@1: var d = new Date(); bgneal@1: d.setDate(parseInt(bits[1], 10)); bgneal@1: d.setMonth(parseMonth(bits[2])); bgneal@1: return d; bgneal@1: } bgneal@1: }, bgneal@1: // 4th Jan 2003 bgneal@1: { re: /^(\d{1,2})(?:st|nd|rd|th)? (\w+),? (\d{4})$/i, bgneal@1: handler: function(bits) { bgneal@1: var d = new Date(); bgneal@1: d.setDate(parseInt(bits[1], 10)); bgneal@1: d.setMonth(parseMonth(bits[2])); bgneal@1: d.setYear(bits[3]); bgneal@1: return d; bgneal@1: } bgneal@1: }, bgneal@1: // Jan 4th bgneal@1: { re: /^(\w+) (\d{1,2})(?:st|nd|rd|th)?$/i, bgneal@1: handler: function(bits) { bgneal@1: var d = new Date(); bgneal@1: d.setDate(parseInt(bits[2], 10)); bgneal@1: d.setMonth(parseMonth(bits[1])); bgneal@1: return d; bgneal@1: } bgneal@1: }, bgneal@1: // Jan 4th 2003 bgneal@1: { re: /^(\w+) (\d{1,2})(?:st|nd|rd|th)?,? (\d{4})$/i, bgneal@1: handler: function(bits) { bgneal@1: var d = new Date(); bgneal@1: d.setDate(parseInt(bits[2], 10)); bgneal@1: d.setMonth(parseMonth(bits[1])); bgneal@1: d.setYear(bits[3]); bgneal@1: return d; bgneal@1: } bgneal@1: }, bgneal@1: // next Tuesday - this is suspect due to weird meaning of "next" bgneal@1: { re: /^next (\w+)$/i, bgneal@1: handler: function(bits) { bgneal@1: var d = new Date(); bgneal@1: var day = d.getDay(); bgneal@1: var newDay = parseWeekday(bits[1]); bgneal@1: var addDays = newDay - day; bgneal@1: if (newDay <= day) { bgneal@1: addDays += 7; bgneal@1: } bgneal@1: d.setDate(d.getDate() + addDays); bgneal@1: return d; bgneal@1: } bgneal@1: }, bgneal@1: // last Tuesday bgneal@1: { re: /^last (\w+)$/i, bgneal@1: handler: function(bits) { bgneal@1: throw new Error("Not yet implemented"); bgneal@1: } bgneal@1: }, bgneal@1: // mm/dd/yyyy (American style) bgneal@1: { re: /(\d{1,2})\/(\d{1,2})\/(\d{4})/, bgneal@1: handler: function(bits) { bgneal@1: var d = new Date(); bgneal@1: d.setYear(bits[3]); bgneal@1: d.setDate(parseInt(bits[2], 10)); bgneal@1: d.setMonth(parseInt(bits[1], 10) - 1); // Because months indexed from 0 bgneal@1: return d; bgneal@1: } bgneal@1: }, bgneal@1: // yyyy-mm-dd (ISO style) bgneal@1: { re: /(\d{4})-(\d{1,2})-(\d{1,2})/, bgneal@1: handler: function(bits) { bgneal@1: var d = new Date(); bgneal@1: d.setYear(parseInt(bits[1])); bgneal@1: d.setMonth(parseInt(bits[2], 10) - 1); bgneal@1: d.setDate(parseInt(bits[3], 10)); bgneal@1: return d; bgneal@1: } bgneal@1: }, bgneal@1: ]; bgneal@1: bgneal@1: function parseDateString(s) { bgneal@1: for (var i = 0; i < dateParsePatterns.length; i++) { bgneal@1: var re = dateParsePatterns[i].re; bgneal@1: var handler = dateParsePatterns[i].handler; bgneal@1: var bits = re.exec(s); bgneal@1: if (bits) { bgneal@1: return handler(bits); bgneal@1: } bgneal@1: } bgneal@1: throw new Error("Invalid date string"); bgneal@1: } bgneal@1: bgneal@1: function fmt00(x) { bgneal@1: // fmt00: Tags leading zero onto numbers 0 - 9. bgneal@1: // Particularly useful for displaying results from Date methods. bgneal@1: // bgneal@1: if (Math.abs(parseInt(x)) < 10){ bgneal@1: x = "0"+ Math.abs(x); bgneal@1: } bgneal@1: return x; bgneal@1: } bgneal@1: bgneal@1: function parseDateStringISO(s) { bgneal@1: try { bgneal@1: var d = parseDateString(s); bgneal@1: return d.getFullYear() + '-' + (fmt00(d.getMonth() + 1)) + '-' + fmt00(d.getDate()) bgneal@1: } bgneal@1: catch (e) { return s; } bgneal@1: } bgneal@1: function magicDate(input) { bgneal@1: var messagespan = input.id + 'Msg'; bgneal@1: try { bgneal@1: var d = parseDateString(input.value); bgneal@1: input.value = d.getFullYear() + '-' + (fmt00(d.getMonth() + 1)) + '-' + bgneal@1: fmt00(d.getDate()); bgneal@1: input.className = ''; bgneal@1: // Human readable date bgneal@1: if (document.getElementById(messagespan)) { bgneal@1: document.getElementById(messagespan).firstChild.nodeValue = d.toDateString(); bgneal@1: document.getElementById(messagespan).className = 'normal'; bgneal@1: } bgneal@1: } bgneal@1: catch (e) { bgneal@1: input.className = 'error'; bgneal@1: var message = e.message; bgneal@1: // Fix for IE6 bug bgneal@1: if (message.indexOf('is null or not an object') > -1) { bgneal@1: message = 'Invalid date string'; bgneal@1: } bgneal@1: if (document.getElementById(messagespan)) { bgneal@1: document.getElementById(messagespan).firstChild.nodeValue = message; bgneal@1: document.getElementById(messagespan).className = 'error'; bgneal@1: } bgneal@1: } bgneal@1: }