How to parse a time into a Date object from user input in JavaScript?

Viewed 75155

I am working on a form widget for users to enter a time of day into a text input (for a calendar application). Using JavaScript (we are using jQuery FWIW), I want to find the best way to parse the text that the user enters into a JavaScript Date() object so I can easily perform comparisons and other things on it.

I tried the parse() method and it is a little too picky for my needs. I would expect it to be able to successfully parse the following example input times (in addition to other logically similar time formats) as the same Date() object:

  • 1:00 pm
  • 1:00 p.m.
  • 1:00 p
  • 1:00pm
  • 1:00p.m.
  • 1:00p
  • 1 pm
  • 1 p.m.
  • 1 p
  • 1pm
  • 1p.m.
  • 1p
  • 13:00
  • 13

I am thinking that I might use regular expressions to split up the input and extract the information I want to use to create my Date() object. What is the best way to do this?

22 Answers

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',
  '1 p.m.','1 p','1pm','1p.m.', '1p', '13:00','13', '1a', '12', '12a', '12p', '12am', '12pm', '2400am', '2400pm', '2400', 
  '1000', '100', '123', '2459', '2359', '2359am', '1100', '123p',
  '1234', '1', '9', '99', '999', '9999', '99999', '0000', '0011', '-1', 'mioaw' ];

for ( var i = 0; i < tests.length; i++ ) {
  console.log( tests[i].padStart( 9, ' ' ) + " = " + parseTime(tests[i]) );
}

It should work for a few other varieties as well (even if a.m. is used, it'll still work - for example). Obviously this is pretty crude but it's also pretty lightweight (much cheaper to use that than a full library, for example).

Warning: The code doe not work with 12:00 AM, etc.

All of the examples provided fail to work for times from 12:00 am to 12:59 am. They also throw an error if the regex does not match a time. The following handles this:

function parseTime(timeString) { 
 if (timeString == '') return null;
 
 var time = timeString.match(/(\d+)(:(\d\d))?\s*(p?)/i); 
 if (time == null) return null;
 
 var hours = parseInt(time[1],10);  
 if (hours == 12 && !time[4]) {
    hours = 0;
 }
 else {
  hours += (hours < 12 && time[4])? 12 : 0;
 } 
 var d = new Date();          
 d.setHours(hours);
 d.setMinutes(parseInt(time[3],10) || 0);
 d.setSeconds(0, 0);  
 return d;
}


var tests = [
  '1:00 pm','1:00 p.m.','1:00 p','1:00pm','1:00p.m.','1:00p','1 pm',
  '1 p.m.','1 p','1pm','1p.m.', '1p', '13:00','13', '1a', '12', '12a', '12p', '12am', '12pm', '2400am', '2400pm', '2400', 
  '1000', '100', '123', '2459', '2359', '2359am', '1100', '123p',
  '1234', '1', '9', '99', '999', '9999', '99999', '0000', '0011', '-1', 'mioaw' ];

for ( var i = 0; i < tests.length; i++ ) {
  console.log( tests[i].padStart( 9, ' ' ) + " = " + parseTime(tests[i]) );
}

This will work for strings which contain a time anywhere inside them. So "abcde12:00pmdef" would be parsed and return 12 pm. If the desired outcome is that it only returns a time when the string only contains a time in them the following regular expression can be used provided you replace "time[4]" with "time[6]".

/^(\d+)(:(\d\d))?\s*((a|(p))m?)?$/i

Don't bother doing it yourself, just use datejs.

Here's an improvement on Joe's version. Feel free to edit it further.

function parseTime(timeString)
{
  if (timeString == '') return null;
  var d = new Date();
  var time = timeString.match(/(\d+)(:(\d\d))?\s*(p?)/i);
  d.setHours( parseInt(time[1],10) + ( ( parseInt(time[1],10) < 12 && time[4] ) ? 12 : 0) );
  d.setMinutes( parseInt(time[3],10) || 0 );
  d.setSeconds(0, 0);
  return d;
}

var tests = [
  '1:00 pm','1:00 p.m.','1:00 p','1:00pm','1:00p.m.','1:00p','1 pm',
  '1 p.m.','1 p','1pm','1p.m.', '1p', '13:00','13', '1a', '12', '12a', '12p', '12am', '12pm', '2400am', '2400pm', '2400', 
  '1000', '100', '123', '2459', '2359', '2359am', '1100', '123p',
  '1234', '1', '9', '99', '999', '9999', '99999', '0000', '0011', '-1', 'mioaw' ];

for ( var i = 0; i < tests.length; i++ ) {
  console.log( tests[i].padStart( 9, ' ' ) + " = " + parseTime(tests[i]) );
}

Changes:

  • Added radix parameter to the parseInt() calls (so jslint won't complain).
  • Made the regex case-insenstive so "2:23 PM" works like "2:23 pm"

I came across a couple of kinks in implementing John Resig's solution. Here is the modified function that I have been using based on his answer:

function parseTime(timeString)
{
  if (timeString == '') return null;
  var d = new Date();
  var time = timeString.match(/(\d+)(:(\d\d))?\s*(p?)/);
  d.setHours( parseInt(time[1]) + ( ( parseInt(time[1]) < 12 && time[4] ) ? 12 : 0) );
  d.setMinutes( parseInt(time[3]) || 0 );
  d.setSeconds(0, 0);
  return d;
} // parseTime()

var tests = [
  '1:00 pm','1:00 p.m.','1:00 p','1:00pm','1:00p.m.','1:00p','1 pm',
  '1 p.m.','1 p','1pm','1p.m.', '1p', '13:00','13', '1a', '12', '12a', '12p', '12am', '12pm', '2400am', '2400pm', '2400', 
  '1000', '100', '123', '2459', '2359', '2359am', '1100', '123p',
  '1234', '1', '9', '99', '999', '9999', '99999', '0000', '0011', '-1', 'mioaw' ];

for ( var i = 0; i < tests.length; i++ ) {
  console.log( tests[i].padStart( 9, ' ' ) + " = " + parseTime(tests[i]) );
}

Compilation table of other answers

First of all, I can't believe that there is not a built-in functionality or even a robust third-party library that can handle this. Actually, it's web development so I can believe it.

Trying to test all edge cases with all these different algorithms was making my head spin, so I took the liberty of compiling all the answers and tests in this thread into a handy table.

The code (and resulting table) is pointlessly large to include inline, so I've made a JSFiddle:

http://jsfiddle.net/jLv16ydb/4/show

// heres some filler code of the functions I included in the test,
// because StackOverfleaux wont let me have a jsfiddle link without code
Functions = [
    JohnResig,
    Qwertie,
    PatrickMcElhaney,
    Brad,
    NathanVillaescusa,
    DaveJarvis,
    AndrewCetinic,
    StefanHaberl,
    PieterDeZwart,
    JoeLencioni,
    Claviska,
    RobG,
    DateJS,
    MomentJS
];
// I didn't include `date-fns`, because it seems to have even more
// limited parsing than MomentJS or DateJS

Please feel free to fork my fiddle and add more algorithms and test cases

I didn't add any comparisons between the result and the "expected" output, because there are cases where the "expected" output could be debated (eg, should 12 be interpreted as 12:00am or 12:00pm?). You will have to go through the table and see which algorithm makes the most sense for you.

Note: The colors do not necessarily indicate quality or "expectedness" of output, they only indicate the type of output:

  • red = js error thrown

  • yellow = "falsy" value (undefined, null, NaN, "", "invalid date")

  • green = js Date() object

  • light green = everything else

Where a Date() object is the output, I convert it to 24 hr HH:mm format for ease of comparison.

This is a more rugged approach that takes into account how users intend to use this type of input. For example, if a user entered "12", they would expect it to be 12pm (noon), and not 12am. The below function handles all of this. It is also available here: http://blog.de-zwart.net/2010-02/javascript-parse-time/

/**
 * Parse a string that looks like time and return a date object.
 * @return  Date object on success, false on error.
 */
String.prototype.parseTime = function() {
    // trim it and reverse it so that the minutes will always be greedy first:
    var value = this.trim().reverse();

    // We need to reverse the string to match the minutes in greedy first, then hours
    var timeParts = value.match(/(a|p)?\s*((\d{2})?:?)(\d{1,2})/i);

    // This didnt match something we know
    if (!timeParts) {
        return false;
    }

    // reverse it:
    timeParts = timeParts.reverse();

    // Reverse the internal parts:
    for( var i = 0; i < timeParts.length; i++ ) {
        timeParts[i] = timeParts[i] === undefined ? '' : timeParts[i].reverse();
    }

    // Parse out the sections:
    var minutes = parseInt(timeParts[1], 10) || 0;
    var hours = parseInt(timeParts[0], 10);
    var afternoon = timeParts[3].toLowerCase() == 'p' ? true : false;

    // If meridian not set, and hours is 12, then assume afternoon.
    afternoon = !timeParts[3] && hours == 12 ? true : afternoon;
    // Anytime the hours are greater than 12, they mean afternoon
    afternoon = hours > 12 ? true : afternoon;
    // Make hours be between 0 and 12:
    hours -= hours > 12 ? 12 : 0;
    // Add 12 if its PM but not noon
    hours += afternoon && hours != 12 ? 12 : 0;
    // Remove 12 for midnight:
    hours -= !afternoon && hours == 12 ? 12 : 0;

    // Check number sanity:
    if( minutes >= 60 || hours >= 24 ) {
        return false;
    }

    // Return a date object with these values set.
    var d = new Date();
    d.setHours(hours);
    d.setMinutes(minutes);
    return d;
}

var tests = [
  '1:00 pm','1:00 p.m.','1:00 p','1:00pm','1:00p.m.','1:00p','1 pm',
  '1 p.m.','1 p','1pm','1p.m.', '1p', '13:00','13', '1a', '12', '12a', '12p', '12am', '12pm', '2400am', '2400pm', '2400', 
  '1000', '100', '123', '2459', '2359', '2359am', '1100', '123p',
  '1234', '1', '9', '99', '999', '9999', '99999', '0000', '0011', '-1', 'mioaw' ];

for ( var i = 0; i < tests.length; i++ ) {
  console.log( tests[i].padStart( 9, ' ' ) + " = " + tests[i].parseTime() );
}

This is a string prototype, so you can use it like so:

var str = '12am';
var date = str.parseTime();

I wasn't happy with the other answers so I made yet another one. This version:

  • Recognizes seconds and milliseconds
  • Returns undefined on invalid input such as "13:00pm" or "11:65"
  • Returns a local time if you provide a localDate parameter, otherwise returns a UTC time on the Unix epoch (Jan 1, 1970).
  • Supports military time like 1330 (to disable, make the first ':' required in the regex)
  • Allows an hour by itself, with 24-hour time (i.e. "7" means 7am).
  • Allows hour 24 as a synonym for hour 0, but hour 25 is not allowed.
  • Requires the time to be at the beginning of the string (to disable, remove ^\s* in the regex)
  • Has test code that actually detects when the output is incorrect.

Edit: it's now a package including a timeToString formatter: npm i simplertime


/**
 * Parses a string into a Date. Supports several formats: "12", "1234",
 * "12:34", "12:34pm", "12:34 PM", "12:34:56 pm", and "12:34:56.789".
 * The time must be at the beginning of the string but can have leading spaces.
 * Anything is allowed after the time as long as the time itself appears to
 * be valid, e.g. "12:34*Z" is OK but "12345" is not.
 * @param {string} t Time string, e.g. "1435" or "2:35 PM" or "14:35:00.0"
 * @param {Date|undefined} localDate If this parameter is provided, setHours
 *        is called on it. Otherwise, setUTCHours is called on 1970/1/1.
 * @returns {Date|undefined} The parsed date, if parsing succeeded.
 */
function parseTime(t, localDate) {
  // ?: means non-capturing group and ?! is zero-width negative lookahead
  var time = t.match(/^\s*(\d\d?)(?::?(\d\d))?(?::(\d\d))?(?!\d)(\.\d+)?\s*(pm?|am?)?/i);
  if (time) {
    var hour = parseInt(time[1]), pm = (time[5] || ' ')[0].toUpperCase();
    var min = time[2] ? parseInt(time[2]) : 0;
    var sec = time[3] ? parseInt(time[3]) : 0;
    var ms = (time[4] ? parseFloat(time[4]) * 1000 : 0);
    if (pm !== ' ' && (hour == 0 || hour > 12) || hour > 24 || min >= 60 || sec >= 60)
      return undefined;
    if (pm === 'A' && hour === 12) hour = 0;
    if (pm === 'P' && hour !== 12) hour += 12;
    if (hour === 24) hour = 0;
    var date = new Date(localDate!==undefined ? localDate.valueOf() : 0);
    var set = (localDate!==undefined ? date.setHours : date.setUTCHours);
    set.call(date, hour, min, sec, ms);
    return date;
  }
  return undefined;
}

var testSuite = {
  '1300':  ['1:00 pm','1:00 P.M.','1:00 p','1:00pm','1:00p.m.','1:00p','1 pm',
            '1 p.m.','1 p','1pm','1p.m.', '1p', '13:00','13', '1:00:00PM', '1300', '13'],
  '1100':  ['11:00am', '11:00 AM', '11:00', '11:00:00', '1100'],
  '1359':  ['1:59 PM', '13:59', '13:59:00', '1359', '1359:00', '0159pm'],
  '100':   ['1:00am', '1:00 am', '0100', '1', '1a', '1 am'],
  '0':     ['00:00', '24:00', '12:00am', '12am', '12:00:00 AM', '0000', '1200 AM'],
  '30':    ['0:30', '00:30', '24:30', '00:30:00', '12:30:00 am', '0030', '1230am'],
  '1435':  ["2:35 PM", "14:35:00.0", "1435"],
  '715.5': ["7:15:30", "7:15:30am"],
  '109':   ['109'], // Three-digit numbers work (I wasn't sure if they would)
  '':      ['12:60', '11:59:99', '-12:00', 'foo', '0660', '12345', '25:00'],
};

var passed = 0;
for (var key in testSuite) {
  let num = parseFloat(key), h = num / 100 | 0;
  let m = num % 100 | 0, s = (num % 1) * 60;
  let expected = Date.UTC(1970, 0, 1, h, m, s); // Month is zero-based
  let strings = testSuite[key];
  for (let i = 0; i < strings.length; i++) {
    var result = parseTime(strings[i]);
    if (result === undefined ? key !== '' : key === '' || expected !== result.valueOf()) {
      console.log(`Test failed at ${key}:"${strings[i]}" with result ${result ? result.toUTCString() : 'undefined'}`);
    } else {
      passed++;
    }
  }
}
console.log(passed + ' tests passed.');

Why not use validation to narrow down what a user can put in and simplify the list to only include formats that can be parsed (or parsed after some tweaking).

I don't think it's asking too much to require a user to put a time in a supported format.

dd:dd A(m)/P(m)

dd A(m)/P(m)

dd

/(\d+)(?::(\d\d))(?::(\d\d))?\s*([pP]?)/ 

// added test for p or P
// added seconds

d.setHours( parseInt(time[1]) + (time[4] ? 12 : 0) ); // care with new indexes
d.setMinutes( parseInt(time[2]) || 0 );
d.setSeconds( parseInt(time[3]) || 0 );

thanks

If you only want seconds here is a one liner

const toSeconds = s => s.split(':').map(v => parseInt(v)).reverse().reduce((acc,e,i) => acc + e * Math.pow(60,i))

After thoroughly testing and investigating through my other compilation answer, I concluded that @Dave Jarvis's solution was the closest to what I felt were reasonable outputs and edge-case-handling. For reference, I looked at what Google Calendar's time inputs reformatted the time to after exiting the text box.

Even still, I saw that it didn't handle some (albeit weird) edge cases that Google Calendar did. So I reworked it from the ground up and this is what I came up with. I also added it to my compilation answer.

// attempt to parse string as time. return js date object
function parseTime(string) {
  string = String(string);

  var am = null;

  // check if "apm" or "pm" explicitly specified, otherwise null
  if (string.toLowerCase().includes("p")) am = false;
  else if (string.toLowerCase().includes("a")) am = true;

  string = string.replace(/\D/g, ""); // remove non-digit characters
  string = string.substring(0, 4); // take only first 4 digits
  if (string.length === 3) string = "0" + string; // consider eg "030" as "0030"
  string = string.replace(/^00/, "24"); // add 24 hours to preserve eg "0012" as "00:12" instead of "12:00", since will be converted to integer

  var time = parseInt(string); // convert to integer
  // default time if all else fails
  var hours = 12,
    minutes = 0;

  // if able to parse as int
  if (Number.isInteger(time)) {
    // treat eg "4" as "4:00pm" (or "4:00am" if "am" explicitly specified)
    if (time >= 0 && time <= 12) {
      hours = time;
      minutes = 0;
      // if "am" or "pm" not specified, establish from number
      if (am === null) {
        if (hours >= 1 && hours <= 12) am = false;
        else am = true;
      }
    }
    // treat eg "20" as "8:00pm"
    else if (time >= 13 && time <= 99) {
      hours = time % 24;
      minutes = 0;
      // if "am" or "pm" not specified, force "am"
      if (am === null) am = true;
    }
    // treat eg "52:95" as 52 hours 95 minutes 
    else if (time >= 100) {
      hours = Math.floor(time / 100); // take first two digits as hour
      minutes = time % 100; // take last two digits as minute
      // if "am" or "pm" not specified, establish from number
      if (am === null) {
        if (hours >= 1 && hours <= 12) am = false;
        else am = true;
      }
    }

    // add 12 hours if "pm"
    if (am === false && hours !== 12) hours += 12;
    // sub 12 hours if "12:00am" (midnight), making "00:00"
    if (am === true && hours === 12) hours = 0;

    // keep hours within 24 and minutes within 60
    // eg 52 hours 95 minutes becomes 4 hours 35 minutes
    hours = hours % 24;
    minutes = minutes % 60;
  }

  // convert to js date object
  var date = new Date();
  date.setHours(hours);
  date.setMinutes(minutes);
  date.setSeconds(0);
  return date;
}

var tests = [
  '1:00 pm','1:00 p.m.','1:00 p','1:00pm','1:00p.m.','1:00p','1 pm',
  '1 p.m.','1 p','1pm','1p.m.', '1p', '13:00','13', '1a', '12', '12a', '12p', '12am', '12pm', '2400am', '2400pm', '2400', 
  '1000', '100', '123', '2459', '2359', '2359am', '1100', '123p',
  '1234', '1', '9', '99', '999', '9999', '99999', '0000', '0011', '-1', 'mioaw' ];

for ( var i = 0; i < tests.length; i++ ) {
  console.log( tests[i].padStart( 9, ' ' ) + " = " + parseTime(tests[i]) );
}

I feel that this is the closest I can get for my needs, but suggestions are welcome. Note: This is American-centric in that it defaults to am/pm for certain patterns:

  • 1 => 13:00 (1:00pm)
  • 1100 => 23:00 (11:00pm)
  • 456 => 16:56 (4:56pm)

I've needed a time parser function and based on some of the answers i ended up with this function

 function parse(time){
  let post_meridiem = time.match(/p/i) !== null;
  let result;
  time = time.replace(/[^\d:-]/g, '');
  let hours = 0;
  let minutes = 0;
  if (!time) return;
  let parts = time.split(':');
  if (parts.length > 2) time = parts[0] + ':' + parts[1];
  if (parts[0] > 59 && parts.length === 2) time = parts[0];
  if (!parts[0] && parts[1] < 60) minutes = parts[1];
  else if (!parts[0] && parts[1] >= 60) return;
  time = time.replace(/^00/, '24');
  time = parseInt(time.replace(/\D/g, ''));
  if (time >= 2500) return;
  if (time > 0 && time < 24 && parts.length === 1) hours = time;
  else if (time < 59) minutes = time;
  else if (time >= 60 && time <= 99 && parts[0]) {
    hours = ('' + time)[0];
    minutes = ('' + time)[1];
  } else if (time >= 100 && time <= 2359) {
    hours = ~~(time / 100);
    minutes = time % 100;
  } else if (time >= 2400) {
    hours = ~~(time / 100) - 24;
    minutes = time % 100;
    post_meridiem = false;
  }
  if (hours > 59 || minutes > 59) return;
  if (post_meridiem && hours !== 0) hours += 12;
  if (minutes > 59) minutes = 59;
  if (hours > 23) hours = 0;
  result = ('' + hours).padStart(2, '0') + ':' + ('' + minutes).padStart(2, '0');
  return result;
}
 var tests = [
   '1:00 pm','1:00 p.m.','1:00 p','1:00pm','1:00p.m.','1:00p','1 pm',
  '1 p.m.','1 p','1pm','1p.m.', '1p', '13:00','13', '1a', '12', '12a', '12p', '12am', '12pm', '2400am', '2400pm', '2400', 
  '1000', '100', '123', '2459', '2359', '2359am', '1100', '123p',
  '1234', '1', '9', '99', '999', '9999', '0000', '0011', '-1', 'mioaw',
  "0820",
  "32",
  "124",
  "1330",
  "130pm",
  "456",
  ":40",
  ":90",
  "12:69",
  "50:90",
  "aaa12:34aaa",
  "aaa50:00aaa",
 ];

    for ( var i = 0; i < tests.length; i++ ) {
      console.log( tests[i].padStart( 9, ' ' ) + " = " + parse(tests[i]) );
    }
also it's on Compilation table of other answers here is a fork Compilation table of other answers

The main upvoted and selected answers were causing trouble for me and outputting ridiculous results. Below is my stab at it which seems to solve all the issues most people were having, including mine. An added functionality to mine is the ability to specify 'am' or 'pm' as a time of day to default to should the user input not specify (e.g. 1:00). By default, it's set to 'pm'.

One thing to note is this function assumes the user wants to (and attempted to) provide a string representing a time input. Because of this, the "input validation and sanitation" only goes so far as to rule out anything that would cause an error, not anything that doesn't necessarily look like a time. This is best represented by the final three test entries in the array towards the bottom of the code snippet.

const parseTime = (timeString, assumedTimeOfDay = "pm") => {
  // Validate timeString input
  if (!timeString) return null

  const regex = /(\d{1,2})(\d{2})?([a|p]m?)?/
  const noOfDigits = timeString.replace(/[^\d]/g, "").length

  if (noOfDigits === 0) return null

  // Seconds are unsupported (rare use case in my eyes, feel free to edit)
  if (noOfDigits > 4) return null

  // Add a leading 0 to prevent bad regex match (i.e. 100 = 1hr 00min, not 10hr 0min)
  const sanitized = `${noOfDigits === 3 ? "0" : ""}${timeString}`
    .toLowerCase()
    .replace(/[^\dapm]/g, "")
  const parsed = sanitized.match(regex)

  if (!parsed) return null

  // Clean up and name parsed data
  const {
    input,
    hours,
    minutes,
    meridian
  } = {
    input: parsed[0],
    hours: Number(parsed[1] || 0),
    minutes: Number(parsed[2] || 0),
    // Defaults to pm if user provided assumedTimeOfDay is not am or pm
    meridian: /am/.test(`${parsed[3] || assumedTimeOfDay.toLowerCase()}m`) ?
      "am" : "pm",
  }

  // Quick check for valid numbers
  if (hours < 0 || hours >= 24 || minutes < 0 || minutes >= 60) return null

  // Convert hours to 24hr format
  const timeOfDay = hours >= 13 ? "pm" : meridian
  const newHours =
    hours >= 13 ?
    hours :
    hours === 12 && timeOfDay === "am" ?
    0 :
    (hours === 12 && timeOfDay === "pm") || timeOfDay === "am" ?
    hours :
    hours + 12

  // Convert data to Date object and return
  return new Date(new Date().setHours(newHours, minutes, 0))
}

const times = [
  '12',
  '12p',
  '12pm',
  '12p.m.',
  '12 p',
  '12 pm',
  '12 p.m.',
  '12:00',
  '12:00p',
  '12:00pm',
  '12:00p.m.',
  '12:00 p',
  '12:00 pm',
  '12:00 p.m.',
  '12:00',
  '12:00p',
  '12:00pm',
  '12:00p.m.',
  '12:00 p',
  '12:00 pm',
  '12:00 p.m.',
  '1200',
  '1200p',
  '1200pm',
  '1200p.m.',
  '1200 p',
  '1200 pm',
  '1200 p.m.',
  '12',
  '1200',
  '12:00',
  '1',
  '1p',
  '1pm',
  '1p.m.',
  '1 p',
  '1 pm',
  '1 p.m.',
  '1:00',
  '1:00p',
  '1:00pm',
  '1:00p.m.',
  '1:00 p',
  '1:00 pm',
  '1:00 p.m.',
  '01:00',
  '01:00p',
  '01:00pm',
  '01:00p.m.',
  '01:00 p',
  '01:00 pm',
  '01:00 p.m.',
  '0100',
  '0100p',
  '0100pm',
  '0100p.m.',
  '0100 p',
  '0100 pm',
  '0100 p.m.',
  '13',
  '1300',
  '13:00',
  'random',
  '092fsd9)*(U243',
  '092fsd9)*(U'
]

times.map(t => {
  const parsed = parseTime(t)

  if (parsed) {
    console.log(`${parsed.toLocaleTimeString()} from ${t}`)
  } else {
    console.log(`Invalid Time (${t})`)
  }
})

Although I've tested this quite a bit, I'm sure I tunnel-visioned on something. If someone is able to break it (in a reasonable way), please comment and I'll see about updating!

Related