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.

2

3 Answers 3

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));
Sign up to request clarification or add additional context in comments.

2 Comments

Is it just me, or is regex always thoroughly disgusting?
@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..
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.

Comments

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.