-2

I am constructing an array using .map, the output is as follows:

renderDataLabels = (labelArray) => {
  const res = Object.values(this.state.totalCalls);
  labelArray = res.map(label => {
    return label.date
  })
  console.log(labelArray);
  return labelArray
}

/// Output: ///

0: "2020-07-11"
1: "2020-07-12"
2: "2020-07-13"
3: "2020-07-14"
4: "2020-07-15"
5: "2020-07-16"
6: "2020-07-17"
7: "2020-07-18"
8: "2020-07-19"
9: "2020-07-20"
10: "2020-07-21"

How can I format the dates before returning the array so that they are formatted as Jul, XX 2020?

jmargolisvt
  • 5,722
  • 4
  • 29
  • 46
Seth Spivey
  • 347
  • 2
  • 4
  • 22

3 Answers3

2

You can use moment.js. Using this library you can format date to any format required.

labelArray = res.map(label => {
    return moment(label.date).format('MMM, DD YYYY')
  })
Muhammad Saqlain
  • 2,112
  • 4
  • 33
  • 48
0
renderDataLabels = (labelArray) => {
  const res = Object.values(this.state.totalCalls);
  labelArray = res.map(label => {
    const dateArray = label.date.split("-");
    const newDate = `${dateArray[1]}-${dateArray[2]}-${dateArray[0]}`;
    return newDate;
  })
  console.log(labelArray);
  return labelArray
}

instead of running to install momentjs, you can use this simply.

Talg123
  • 1,468
  • 1
  • 10
  • 15
0

I hope its helpful for you.

const myDate = new Date('2020-04-18')

const dateFormated = new Intl.DateTimeFormat('en', { year: 'numeric', month: 'short', day: '2-digit' }) 
const [{ value: month },,{ value: day },,{ value: year }] = dateFormated .formatToParts( myDate ) 

console.log(`${month}, ${day} ${year}`)
GMKHussain
  • 3,342
  • 1
  • 21
  • 19