Chartjs-How to format date parsed from ASPX JSON via AJAX?

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))

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"

1๐Ÿ‘

You also can use momentjs library.

var yourdate = "/Date(154564434687)/";
moment(Number(yourdate.match(/\d+/)[0])).format('MM/DD/YYYY')
"11/25/1974"

Leave a comment