var string = '1: Mode: SOME Date range: 01/01/2018-31/12/2018 User: HANS'
I would like to get a date from the above string, remove all the other content and return like the below:
['2018-01-01', '2018-12-31', '201801', '201812']
Basically, YYYY-MM-DD, YYYY-MM-DD, YYYMM, YYYYMM.
I have tried the following but couldn’t progress more:
var res = string.match(/d{2}([/.-])d{2}1d{4}/g);
=> ['01/01/2018', '31/12/2018']
Then another function is to get the YYYYMM part.
var arr = res[0].split("-");
return arr[0]+arr[1];
=> '201801'
Is it possible to get the four elements in a single array in an efficient way?
Thanks in advance!
6
Answers
I would do it in two steps. Find the dates, then format them.
I know this has already been answered, but this answer may be more readable / clean
This should fix your issue.
Code:
But if you want, you can extract everything in one step.