Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations bkrike on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Regex and a Time format

Status
Not open for further replies.

psappenfield

Programmer
Aug 10, 2004
14
US
Hello,

I am trying to get a regex to work for the following time format: 2:00PM-2:15PM

Code:
/^([1-9]|0[1-9]|1[012])[:]([0-5][0-9])([AM|PM])[-]([1-9]|0[1-9]|1[012])[:]([0-5][0-9])([AM|PM])+$/

I am fairly new to regex, but I copied the time condition from a previous regex that does work correctly.

Help please...

[banghead]
 
this works for the format:
2:00PM-2:15PM

with optional leading zeroes with times < 10, optional whitespace between the time and AM/PM indicators, case-insensitive AM/PM indicators, and optional whitespace around the hyphen
Code:
var pattern = /^((0?[123456789])|(1[012]))\s*:\s*([012345]\d)(\s*:\s*([012345]\d))?\s*[ap]m\s*-\s*((0?[123456789])|(1[012]))\s*:\s*([012345]\d)(\s*:\s*([012345]\d))?\s*[ap]m$/i;

-jeff
try { succeed(); } catch(E) { tryAgain(); } finally { rtfm(); }
i like your sleeves...they're real big
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top