[Fixed] how to fetch date from list of data in angular?

Issue

I have a list of json coming from api in which date is present as shown below. How can i extract this date. I have just shown random data that is data number may be 1 or 2 or 3 digits. Here in this case we have data number 24,124,04. Date is in yyyy-mm-dd format.

data-24-1296176045661093905-33-2021-03-10T12-22-47.26942346.json
data-124-1296176045661093905-33-2021-05-14T12-22-47.26942346.json
data-04-1296176045661093905-33-2021-09-19T12-22-47.26942346.json

Since this list is dynamic, so data number may change. How can i get date from it???

Solution

I can think of this right now. This will work well for the stated filenames:

const arr = ['data-24-1296176045661093905-33-2021-03-10T12-22-47.26942346.json',
'data-124-1296176045661093905-33-2021-05-14T12-22-47.26942346.json',
'data-04-1296176045661093905-33-2021-09-19T12-22-47.26942346.json']
const getDate = (fileName) => {
    return fileName.split(/(?<=\-)(.*?)(?=\T)/)[1].split('-').slice(3).join('-');
}

arr.forEach(fileName => console.log(getDate(fileName)));

Leave a Reply

(*) Required, Your email will not be published