3

I have an application that returns JSON date to me which I display on a Highcharts bar graph. The field for the x-axis is a date and it comes back from the server is JSON format such as :"date":"2016-11-15T00:00:00.000Z"

I want to display the date on the x-axis as just 2016-11-15, or even better, display it in local date format, which in this case would be 2016-11-14 as it is GMT-5 hours.

I've read the documentation, but it doesn't seem to work. Any help is appreciated!!

user2843365
  • 377
  • 2
  • 7
  • 20

2 Answers2

10

You can parse ISO string date to js date with the Date constructor. And then get timestamp in milisecond - which is a required format for Highcharts.

new Date("2016-11-15T00:00:00.000Z").getTime(),

In the format property, you can define how the date should be displayed.

 xAxis: {
  type: 'datetime',
  labels: {
    format: '{value:%Y-%m-%d}',
  }
},

example: http://jsfiddle.net/akehpzq4/4/

For a local time, see useUTC option.

morganfree
  • 12,254
  • 1
  • 14
  • 21
0

You can use this function to return format date

function ReadDate(dateTM) {
    var date = new Date(parseInt(dateTM.replace("/Date(", "").replace(")/", ""), 10));
    if (date.getFullYear() > 1 && date.getFullYear() != 1900) {
        return (date.getDate() + '/' + (date.getMonth() + 1) + '/' + date.getFullYear());
    } else return ""
}

You can read guide at this link How do I format a Microsoft JSON date?

Community
  • 1
  • 1
Bader
  • 33
  • 6