I have a dataset with date format as
var dataset = [{ “monthDate”: “2018-05”, “count”: 83 }, { “monthDate”: “2018-06”, “count”: 23 },…..]
I wish to change this to ‘May-18’, ‘June-18’ and so on and pass this data to Highchart Categories. How do I do that?
Advertisement
Answer
You could parse the date into a Date object, and then format it with toLocaleDateString
. One adjustment is needed at the end, to get the hyphen in the output:
JavaScript
x
10
10
1
var dataset = [{ "monthDate": "2018-05", "count": 83 }, { "monthDate": "2018-06", "count": 23 }];
2
3
var result = dataset.map(o => ({
4
monthDate: new Date(parseInt(o.monthDate), o.monthDate.slice(-2) - 1)
5
.toLocaleDateString("en", {month: "long", year: "2-digit"})
6
.replace(" ", "-"),
7
count: o.count
8
}));
9
10
console.log(result);