How to parse a time into a Date object from user input in JavaScript?
A quick solution which works on the input that you’ve specified: function parseTime( t ) { var d = new Date(); var time = t.match( /(\d+)(?::(\d\d))?\s*(p?)/ ); d.setHours( parseInt( time[1]) + (time[3] ? 12 : 0) ); d.setMinutes( parseInt( time[2]) || 0 ); return d; } var tests = [ ‘1:00 pm’,’1:00 p.m.’,’1:00 p’,’1:00pm’,’1:00p.m.’,’1:00p’,’1 pm’, … Read more