Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Filter date by month with different format using javascript

I have the following relTotal array.

const relTotal = [[11111,15 / 05 / 2024,11:11hs,teste de a3,admin],
[12541,03 / 06 / 2024,11:28hs,teste de a3,admin],
[151515,22 / 03 / 2024,15:15hs,testes,testetes],
[151515,22 / 05 / 2024,15:15hs,testes,testetes]]

The Date field is formatted as "dd / MM / yyyy" with spaces because I need this format for my application. My question is how do I filter this array using only the month MM as a reference?

If I use includes and pass the value "03" as a parameter, it will show all the lines where it appears and that’s not what I need

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

I tried using the following code

let retFilter = relTotal.filter(month=> month.includes("03"));
console.log(retFilter);

but it’s not filtering the way I need

>Solution :

Use String#includes():

const relTotal = [[11111,'15 / 05 / 2024','11:11hs','teste de a3','admin'],
[12541,'03 / 06 / 2024','11:28hs','teste de a3','admin'],
[151515,'22 / 03 / 2024','15:15hs','testes','testetes'],
[151515,'22 / 05 / 2024','15:15hs','testes','testetes']]

console.log(
relTotal.filter(el => el[1].includes('/ 03'))
)
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading