How to fetch all dates and time from string using JavaScript/Jquery?

How to fetch all dates and time from string using JavaScript/Jquery?



I'm trying to fetch all DateTime fields using JS/Jquery.



Currently I'm trying it with Regex, but failing miserably.



INPUT:


12345 User Name 9/10/2018 11:39:37 AM Valid Entry Place1
12345 User Name 9/10/2018 12:48:43 PM Valid Exit Outside Place1
12345 User Name 9/10/2018 1:00:44 PM Valid Entry Place1
12345 User Name 9/10/2018 2:17:01 PM Valid Exit Outside Place1
12345 User Name 9/10/2018 3:23:36 PM Valid Entry Place1
12345 User Name 9/10/2018 3:25:56 PM Valid Entry Place1
12345 User Name 9/10/2018 6:06:25 PM Valid Exit Outside Place1
12345 User Name 9/10/2018 6:07:55 PM Valid Entry LC
12345 User Name 9/10/2018 6:28:19 PM Valid Exit Outside LC
12345 User Name 9/10/2018 6:30:06 PM Valid Entry Place1



EXPECTED OUTPUT:


9/10/2018 11:39:37 AM
9/10/2018 12:48:43 PM
9/10/2018 1:00:44 PM
9/10/2018 2:17:01 PM
9/10/2018 3:23:36 PM
9/10/2018 3:25:56 PM
9/10/2018 6:06:25 PM
9/10/2018 6:07:55 PM
9/10/2018 6:28:19 PM
9/10/2018 6:30:06 PM



CURRENT LOGIC:


function myFunction()
var str = document.getElementById("demo").innerHTML;
var txtIdAndName = str.replace(/[0-9]*s[a-z,s*]*/ig,"");
var txtStatusAndPlace = txtIdAndName.replace(/[AM,PM]+s[a-z,s*]*/ig,"<br/>");
document.getElementById("demo").innerHTML = txtStatusAndPlace;



Please do let me know if any more info is needed form my end.



Can use any Logic or method to achieve the task.



Thanks in advance




3 Answers
3



You may match these substrings with


s.match(/bd1,2/d1,2/d4s+d1,2:d2:d2s*[AP]Mb/g)



See the regex demo



Details


b


d1,2


/


/


d1,2/d4


/


s+


d1,2:d2:d2


:


:


s*


[AP]


A


P


M


M


b



JS demo:




var s = "12345 User Name 9/10/2018 11:39:37 AM Valid Entry Place1 n12345 User Name 9/10/2018 12:48:43 PM Valid Exit Outside Place1 n12345 User Name 9/10/2018 1:00:44 PM Valid Entry Place1 n12345 User Name 9/10/2018 2:17:01 PM Valid Exit Outside Place1 n12345 User Name 9/10/2018 3:23:36 PM Valid Entry Place1 n12345 User Name 9/10/2018 3:25:56 PM Valid Entry Place1 n12345 User Name 9/10/2018 6:06:25 PM Valid Exit Outside Place1 n12345 User Name 9/10/2018 6:07:55 PM Valid Entry LC n12345 User Name 9/10/2018 6:28:19 PM Valid Exit Outside LC n12345 User Name 9/10/2018 6:30:06 PM Valid Entry Place1";
var rx = /bd1,2/d1,2/d4s+d1,2:d2:d2s*[AP]Mb/g;
document.body.innerHTML = "<pre>" + s.match(rx).join("<br/>") + "</pre>";






changing d2 to d1,2 in date will also match 12345 User Name 9/7/2018 12:48:43 PM Valid Exit Outside Place1

– RAN_0915
Sep 11 '18 at 7:31


12345 User Name 9/7/2018 12:48:43 PM Valid Exit Outside Place1






@NambiMurugan Fair enough, I added the minimum limit to the quantifier.

– Wiktor Stribiżew
Sep 11 '18 at 7:33






@Wiktor Stribiżew This worked for me. Thanks a ton for the extra effort put in.

– theLearner
Sep 11 '18 at 7:34



Try this regex,


d+/d+/d+s*d+:d+:d+s*[AP]M



Demo






Note that s*? will yield the same result as s* since d+ (pattern after) does not match a whitespace.

– Wiktor Stribiżew
Sep 11 '18 at 7:34


s*?


s*


d+



Other users posted answer for using regex. But you can use another way instead of regex if structure of your string is constant.



Using javascript .split() to splitting string and using .splice() to selecting special part of result.


.split()


.splice()




var str = document.getElementById("demo").innerHTML;
var newStr = str.trim().split("n").map(function(text)
return text.split(" ").splice(3, 3).join(" ");
).join("n");
console.log(newStr);


<div id="demo">
12345 User Name 9/10/2018 11:39:37 AM Valid Entry Place1
12345 User Name 9/10/2018 12:48:43 PM Valid Exit Outside Place1
12345 User Name 9/10/2018 1:00:44 PM Valid Entry Place1
12345 User Name 9/10/2018 2:17:01 PM Valid Exit Outside Place1
12345 User Name 9/10/2018 3:23:36 PM Valid Entry Place1
12345 User Name 9/10/2018 3:25:56 PM Valid Entry Place1
12345 User Name 9/10/2018 6:06:25 PM Valid Exit Outside Place1
12345 User Name 9/10/2018 6:07:55 PM Valid Entry LC
12345 User Name 9/10/2018 6:28:19 PM Valid Exit Outside LC
12345 User Name 9/10/2018 6:30:06 PM Valid Entry Place1
</div>






Yes, even this option works great. Thanks for the reply

– theLearner
Sep 11 '18 at 7:41



Thanks for contributing an answer to Stack Overflow!



But avoid



To learn more, see our tips on writing great answers.



Required, but never shown



Required, but never shown




By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Popular posts from this blog

𛂒𛀶,𛀽𛀑𛂀𛃧𛂓𛀙𛃆𛃑𛃷𛂟𛁡𛀢𛀟𛁤𛂽𛁕𛁪𛂟𛂯,𛁞𛂧𛀴𛁄𛁠𛁼𛂿𛀤 𛂘,𛁺𛂾𛃭𛃭𛃵𛀺,𛂣𛃍𛂖𛃶 𛀸𛃀𛂖𛁶𛁏𛁚 𛂢𛂞 𛁰𛂆𛀔,𛁸𛀽𛁓𛃋𛂇𛃧𛀧𛃣𛂐𛃇,𛂂𛃻𛃲𛁬𛃞𛀧𛃃𛀅 𛂭𛁠𛁡𛃇𛀷𛃓𛁥,𛁙𛁘𛁞𛃸𛁸𛃣𛁜,𛂛,𛃿,𛁯𛂘𛂌𛃛𛁱𛃌𛂈𛂇 𛁊𛃲,𛀕𛃴𛀜 𛀶𛂆𛀶𛃟𛂉𛀣,𛂐𛁞𛁾 𛁷𛂑𛁳𛂯𛀬𛃅,𛃶𛁼

Edmonton

Crossroads (UK TV series)