javascript - Get time from date and time using Regex -


how extract 12:05 am 7/16/2016 12:05:00 am using regex?

i've made far

test = "7/16/2016 12:05:00 am" test.match(/^(\s+) (.*)/)[2] > "12:05:00 am" 

but can't figure out how remove seconds. plus, if there's simpler/more efficient way of doing i'm trying do, please let me know.

i rather not rely on third-party libraries moment.js


note: desired output 12:05 am , not 12:05

look explicitly digits , colon, lopping off last 2 before matching am/pm. make second hour digit optional, in case matching against "3:14:16 pm":

var test = "7/16/16 12:05:00 am"; var matches = test.match(/(\d\d?:\d\d):\d\d(\s[ap]m)/i); var time = matches && (matches[1] + matches[2]); // time === "12:05 am" 

just note fullness of regex, use curly braces determine number of digits count (i didn't above, because it's more characters in end when it's 1-2). both following , above match same string:

var matches = test.match(/(\d{1,2}:\d{2}):\d{2}(\s[ap]m)/i; 

Comments

Popular posts from this blog

javascript - Slick Slider width recalculation -

jsf - PrimeFaces Datatable - What is f:facet actually doing? -

angular2 services - Angular 2 RC 4 Http post not firing -