Home > other >  Split dates in array with dash (-) and take first number from split result
Split dates in array with dash (-) and take first number from split result

Time:07-26

I have one array with dates having format as a DD-MM-YYYY. I have to take only DD i.e. days from it and push in another array.

I am trying using split function to separate out it but not able to push into array.

  let arrayData = ["15-07-2022", "18-07-2022", "20-07-2022"];
  
  let pushData = [];
        arrayData.map(x => (
            pushData.push(x.split('-'))
     ))
     
     //output should be [15,18,20]

CodePudding user response:

You can use Array.map() to achieve the desired result. Note I use to convert the string to numbers as shown in your expected output.

let arrayData = ["15-07-2022", "18-07-2022", "20-07-2022"];
const result = arrayData.map((date) =>  date.split('-')[0])
console.log(result)

  • Related