1

I have a datetime in SQL server that i return from a controller method in my MVC project using return json()

I get this format in my json-response:

time: "/Date(1409763303817)/"

I try to use this data in a table in my UI with this code:

$("#missingTime").html(new Date(data3.time).toDateString());

I get "Invalid Date" in my column.

What am i doing wrong?

Edit: Found a solution

new Date(parseInt(data3.time.replace("/Date(", "").replace(")/",""), 10)
5
  • Have a look at stackoverflow.com/questions/206384/… Commented Sep 4, 2014 at 15:21
  • Why don't you accept an answer? People take their time to help, the least you can do is play the game by the rules in return. Commented Sep 8, 2014 at 9:47
  • @SheedySheedySheedy you'r answer didn't solve it. I will add the solution i found. Commented Sep 11, 2014 at 12:34
  • The code in my answer does solve your problem. I tested it at the time and here is a FIDDLE to show you that it solves it. Commented Sep 11, 2014 at 20:13
  • 1
    It still doesnt work in my project, but it works in the fiddle, so i'll accept ur answer. Commented Sep 12, 2014 at 10:55

2 Answers 2

2

The JSON value you have is not a valid number to be parsed into a JavaScript Date object. A quick fix would be to strip out the UTC value (the numbers) from your string using a regular expression and pass this to your function (after parsing into a number), like so:

var regEx = /\d+/g;
var utcInfo = data3.time.match(regEx);
$("#missingTime").html(new Date(parseInt(utcInfo)).toDateString());

Although you might want to check why your JSON response is giving you the incorrect value in the first place. The value in the JSON object needs to be as follows in order for your JS code to work:

time: 1409763303817
Sign up to request clarification or add additional context in comments.

6 Comments

What should i change on the serverside? Time is a regular datetime in my object, public System.DateTime time { get; set; }. The object is returned as json with MVCs return json(object).
Do you mean .Net MVC?
Yes, a regular MVC 4 project
Then, with the greatest of respect, that is no longer a JavaScript question. I have no knowledge of that framework. What you need to pass to the JavaScript Date function is a number representing the UTC. That is the set of numbers which is represented in the string you have now. How you get that out of the database is another issue entirely.
I have edited my answer to show you what value you need to get back from the database in your JSON object, in order for your present JS code to work.
|
0

If you take use of the MVC-Model the Controller-Layer should convert the time into a format the View can handle.

See, a Time is more than Minutes, Hours and Seconds. Its depends on the Timezone the calling Browser physicaly is.

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.