0

Actually i'm parsing some data from MySQL then i serialize them as JSON. The next step is where i retrive the JSON via AJAX and put the data in a Chart.JS.

The issue is that the date values are formatted as

/Date(154564434687)/

Here is the JQuery code where i set the values to the chart from parsed JSON

 function GetMOV() {

        $.ajax({
            type: "post",
            url: "index.aspx/GetMOV",
            contentType: "application/json; charset=utf-8",
            dataType: "json",
            success: function (r) {

                data = r.d;
                data = jQuery.parseJSON(data)

                new Chart(document.getElementById("linechart"), {
                    type: 'line',
                    data: {
                        labels: data.map(f => f.text),
                        datasets: [
                            {
                                label: "Venduto",
                                data: data.map(f => f.value),
                                fill: false,
                                borderColor: 'rgba(75,192,192,1)',
                                lineTension: 0.1,
                                borderWidth: 3,
                                pointBackgroundColor: '#fff',
                                pointBorderWidth: 1,
                                pointHoverRadius: 5,
                                borderCapStyle: 'square',
                                borderJoinStyle: 'square',
                                pointHitRadius: 20,
                                pointStyle: 'circle',
                            }
                        ]
                    },
                    options: {
                        legend: { display: false },
                        scales: {
                            yAxes: [{
                                ticks: {
                                    beginAtZero: true,
                                    min: 0
                                },
                                time: {
                                    unit: 'year'
                                }
                            }]
                        }
                    }
                });

            },
            error: function (error) {
                OnFailure(error);
                alert('Error');
            }
        });
    }
NiceToMytyuk
  • 3,644
  • 3
  • 39
  • 100

3 Answers3

2

Using map keyword with function

I have just tried some line of codes. Do you want like this?

var data = [{value: 4111.47, text: "/Date(1540159200000)/"} ,{value: 5122.85, text: "/Date(1540245600000)/"} ,{value: 3906.24, text: "/Date(1540332000000)/"} ,{value: 3749.79, text: "/Date(1540418400000)/"} , {value: 6349.68, text: "/Date(1540504800000)/"}];
function ToJavaScriptDate(value) {
  var pattern = /Date\(([^)]+)\)/;
  var results = pattern.exec(value);
  var dt = new Date(parseFloat(results[1]));
  return (dt.getMonth() + 1) + "/" + dt.getDate() + "/" + dt.getFullYear();
}
data.map(c=>ToJavaScriptDate(c.text))
Hien Nguyen
  • 24,551
  • 7
  • 52
  • 62
1

You can use this function

function ToJavaScriptDate(value) {
  var pattern = /Date\(([^)]+)\)/;
  var results = pattern.exec(value);
  var dt = new Date(parseFloat(results[1]));
  return (dt.getMonth() + 1) + "/" + dt.getDate() + "/" + dt.getFullYear();
}

ToJavaScriptDate("/Date(154564434687)/")
"11/25/1974"
Hien Nguyen
  • 24,551
  • 7
  • 52
  • 62
  • Can you put your full data here? – Hien Nguyen Feb 07 '19 at 14:29
  • 10: {value: 4111.47, text: "/Date(1540159200000)/"} 11: {value: 5122.85, text: "/Date(1540245600000)/"} 12: {value: 3906.24, text: "/Date(1540332000000)/"} 13: {value: 3749.79, text: "/Date(1540418400000)/"} 14: {value: 6349.68, text: "/Date(1540504800000)/"} – NiceToMytyuk Feb 07 '19 at 14:30
1

You also can use momentjs library.

var yourdate = "/Date(154564434687)/";
moment(Number(yourdate.match(/\d+/)[0])).format('MM/DD/YYYY')
"11/25/1974"
Hien Nguyen
  • 24,551
  • 7
  • 52
  • 62