简体   繁体   中英

how to fetch date from list of data in angular?

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???

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)));

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM