bgneal@1: var timeParsePatterns = [ bgneal@1: // 9 bgneal@1: { re: /^\d{1,2}$/i, bgneal@1: handler: function(bits) { bgneal@1: if (bits[0].length == 1) { bgneal@1: return '0' + bits[0] + ':00'; bgneal@1: } else { bgneal@1: return bits[0] + ':00'; bgneal@1: } bgneal@1: } bgneal@1: }, bgneal@1: // 13:00 bgneal@1: { re: /^\d{2}[:.]\d{2}$/i, bgneal@1: handler: function(bits) { bgneal@1: return bits[0].replace('.', ':'); bgneal@1: } bgneal@1: }, bgneal@1: // 9:00 bgneal@1: { re: /^\d[:.]\d{2}$/i, bgneal@1: handler: function(bits) { bgneal@1: return '0' + bits[0].replace('.', ':'); bgneal@1: } bgneal@1: }, bgneal@1: // 3 am / 3 a.m. / 3am bgneal@1: { re: /^(\d+)\s*([ap])(?:.?m.?)?$/i, bgneal@1: handler: function(bits) { bgneal@1: var hour = parseInt(bits[1]); bgneal@1: if (hour == 12) { bgneal@1: hour = 0; bgneal@1: } bgneal@1: if (bits[2].toLowerCase() == 'p') { bgneal@1: if (hour == 12) { bgneal@1: hour = 0; bgneal@1: } bgneal@1: return (hour + 12) + ':00'; bgneal@1: } else { bgneal@1: if (hour < 10) { bgneal@1: return '0' + hour + ':00'; bgneal@1: } else { bgneal@1: return hour + ':00'; bgneal@1: } bgneal@1: } bgneal@1: } bgneal@1: }, bgneal@1: // 3.30 am / 3:15 a.m. / 3.00am bgneal@1: { re: /^(\d+)[.:](\d{2})\s*([ap]).?m.?$/i, bgneal@1: handler: function(bits) { bgneal@1: var hour = parseInt(bits[1]); bgneal@1: var mins = parseInt(bits[2]); bgneal@1: if (mins < 10) { bgneal@1: mins = '0' + mins; bgneal@1: } bgneal@1: if (hour == 12) { bgneal@1: hour = 0; bgneal@1: } bgneal@1: if (bits[3].toLowerCase() == 'p') { bgneal@1: if (hour == 12) { bgneal@1: hour = 0; bgneal@1: } bgneal@1: return (hour + 12) + ':' + mins; bgneal@1: } else { bgneal@1: if (hour < 10) { bgneal@1: return '0' + hour + ':' + mins; bgneal@1: } else { bgneal@1: return hour + ':' + mins; bgneal@1: } bgneal@1: } bgneal@1: } bgneal@1: }, bgneal@1: // noon bgneal@1: { re: /^no/i, bgneal@1: handler: function(bits) { bgneal@1: return '12:00'; bgneal@1: } bgneal@1: }, bgneal@1: // midnight bgneal@1: { re: /^mid/i, bgneal@1: handler: function(bits) { bgneal@1: return '00:00'; bgneal@1: } bgneal@1: } bgneal@1: ]; bgneal@1: bgneal@1: function parseTimeString(s) { bgneal@1: for (var i = 0; i < timeParsePatterns.length; i++) { bgneal@1: var re = timeParsePatterns[i].re; bgneal@1: var handler = timeParsePatterns[i].handler; bgneal@1: var bits = re.exec(s); bgneal@1: if (bits) { bgneal@1: return handler(bits); bgneal@1: } bgneal@1: } bgneal@1: return s; bgneal@1: }