javascript - conversion of string containing times to 24-hour times I can do math with - jQuery -
i trying find way in jquery/javascript reliably convert strings these: "10:30 – 11:00 pm" or "6:45 – 9:50 pm" 2 pieces each, 1030 , 2300 or 645 , 2150 respectively. end goal see if current time in between two, think have part down, conversion 24-hour time throwing me off. here (non)working example might better illustrate idea: http://codepen.io/alexbezuska/pen/lkxbb thanks!
try regex magic:
var str = '6:45 – 9:50 pm'; var result = [], regex = /(\d{1,2}):(\d{1,2})\s?(am|pm)/gi; str.replace(regex, function(_, hours, minutes, meridian) { hours =+ hours; if (meridian.tolowercase() == 'pm') hours += 12; result.push( +(hours +''+ minutes)); }); console.log(result); //=> [645, 2150]
Comments
Post a Comment