I added a few things as an improvement to the accepted answer by @thebreiflabb to suit my use case and thought i'd share.
if the regex is changed to timeReg = /(\d+)[\.|:](\d+)\s?(\w+)/;, it'll handle a few other common cases.
namely:
- using a colon instead of decimal point between hours and minutes
- allowing am/pm to immediately follow the time with no space
also, setting the seconds to 0 (since that was my main use case)
the resulting code would be:
var test, parts, hours, minutes, date, d = (new Date()).getTime(), tests = ['01.25 PM', '01:25pm', '1:25 PM', '11.35 PM', '12.45 PM', '01.25 AM', '11.35 AM', '12.45 AM'], i = tests.length, timeReg = /(\d+)[\.|:](\d+)\s?(\w+)/; for(; i-- > 0;) { test = tests[i]; parts = test.match(timeReg); hours = /am/i.test(parts[3]) ? function(am) {return am < 12 ? am : 0}(parseInt(parts[1], 10)) : function(pm) {return pm < 12 ? pm + 12 : 12}(parseInt(parts[1], 10)); minutes = parseInt(parts[2], 10); date = new Date(d); date.setHours(hours); date.setMinutes(minutes); date.setSeconds(0); console.log(test + ' => ' + date); }