19

I know that my question is similar to others but I didn't found any solution to my problem.

I have a C# DateTime property

 public DateTime MyDate { get;set;}

When I use an ajax to get some information, I wrote in javascript something like:

$.each(object, function(k,v){
  alert(object.MyDate);
});

It returns something like:

/Date(1362478277517)/

It is possible to convert that datetime to javascript date ?

Thank you.

Snake Eyes
  • 16,287
  • 34
  • 113
  • 221

3 Answers3

29

new Date(object.MyDate); should work.

EDIT: var date = new Date(parseInt(object.MyDate.substr(6)));

I've also seen this method:

var milli = "/Date(1245398693390)/".replace(/\/Date\((-?\d+)\)\//, '$1');
var d = new Date(parseInt(milli));
Rob
  • 4,927
  • 12
  • 49
  • 54
  • 6
    Is it just me, or is regex always thoroughly disgusting? – Grant Thomas Apr 05 '13 at 08:56
  • 4
    @GrantThomas Actually i agree, i don't think I've ever used regex anywhere. It's disgusting to read and it doesn't feel right or "clean" to me at all.. – Rob Apr 05 '13 at 09:02
0

I'm using .Net Core 2.0. & MySQL 5.7

In my current development, I'm assigning the returned value directly into the DOM object like this:

DOMControl.value = response.CreatedOn.toString().split(".")[0];

I'm returning a JsonResult of the resulting object, the resulting JSON arrives with the date value as follows:

{
  ...
  createdOn : "2017-11-28T00:43:29.0472483Z"
  ...
}

I hope this help to somebody.

Henry Rodriguez
  • 805
  • 7
  • 11
0

When you send a DateTime type to a javascript client, it is converted to a string enclosed as follows /Date(xxx)/ where xxx is the date and time in milliseconds in Unix time. To convert it, you need to remove everything except the xxx as follows:

var date = new Date(parseInt(myCSDateTime.substr(6)));

Dean P
  • 1,841
  • 23
  • 23