Javascript serialization of DateTime in asp.net is not giving a javascript date object?
44,772
Solution 1
This seems to work (Thanks Chris S for the idea). In the C# do a replace to remove the string wrapper from around the date object;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.Script.Serialization;
using System.Web.Script.Services;
using System.Web.Services;
using System.Web.UI.WebControls;
namespace test
{
[ScriptService]
public partial class testing: System.Web.UI.Page
{
protected string strCaseID;
protected string jsonCase;
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
strCaseID =Tools.GetQueryObject("id");
// get a complex object with dates, string, arrays etc.
jsonESHACase = new JavaScriptSerializer().Serialize(objCase.Get(strCaseID ))
.Replace("\"\\/Date(", "new Date(").Replace(")\\/\"", ")");
}
}
}
}
..and after removing the quotes and adding the new prefix to Date this js now works. Hooray!
testCase= <%= jsonESHACase %>;
if (testCase) {
document.write(testCase["ClosingDate"].format("MM dd yyyy"));
}
Solution 2
This is a known limitation with JSON. This answer might help you, specifically:
value = new Date(parseInt(value.replace("/Date(", "").replace(")/",""), 10));
Solution 3
Simple javascript manipulation like this:
function(param){
var date = new Date(parseInt(param.substr(6)));
return date;
}
Pass in JSON date as param to the function and it will return a javascript date.
Solution 4
With a little string manipulation and an eval you can create a Date object
var dteNow = "\/Date(1249335477787)\/";
var dteObj = eval("new " + dteNow.replace(/\//g,""));
Solution 5
Slightly simpler string clean up with RegEx:
var myDate = "\\/Date(1508821200000)\/";
var jsDate = new Date(parseInt(myDate.replace(/\D/g, '')));
Related videos on Youtube
Author by
Jenn Briden
Updated on July 09, 2022Comments
-
Jenn Briden almost 2 years
When I parse a DateTime to json in .Net it returns a string (i.e.
"\/Date(1249335194272)\/"
). How do I make it return a js Date object constructor not wrap in a string?// js server code var dteNow = <%= jsonDateNow %>; // js rendered code var dteNow = "\/Date(1249335477787)\/"; // C# using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.Web.UI; using System.Web.Script.Serialization; using System.Web.UI.WebControls; namespace testing{ public partial class iTaxPrep : System.Web.UI.Page { protected string jsonDateNow; protected void Page_Load(object sender, EventArgs e) { if (!IsPostBack) { jsonDateNow = new JavaScriptSerializer().Serialize(DateTime.Now); } } } }
-
Jenn Briden almost 15 yearsThe actually problem is that I need to serialize objects to JSON that contain dates and use those dates client-side.
-
Jenn Briden almost 15 yearsThat is pretty much what I ended up doing but when I parse a large object containing several dates and other info this will start looking ugly.
-
Carl Sharman over 11 yearsI much prefer this answer to the other answers that fix the problem in JS. The problem is in .Net, so should be resolved in .Net IMO.
-
Flexo about 11 yearsA lot of your answers seem to contain very little content beyond just a link to your blog post. Any chance you can expand upon them to make them into real answers? The rest look great and I'm sure the blog posts will make great answers as well.
-
ruffin almost 11 yearseval is evil etc etc, but otherwise nicely done.
-
dhruvpatel over 9 yearsThis works fine for me: value = new Date(parseInt(value.replace("/Date(", "").replace(")/",""))); I guess the 10 in the second arguments not necessary as it appends time 10:00 in the date. Dates I am working on are coming from server and they have the time configured in it, so I don't want that to be changed. Thanks for your answer though!
-
inki over 9 yearsThe 10 in the second argument is necessary, it is the radix argument for parseInt. It does not append anything. It specifies the type the radix or base, and without it may behave differently on different systems.
-
MordechayS over 9 years@webdeveloper the parseInt argument is in
Javascript The Good Parts
(definitely worth a read) - it's one of the many hidden features of Javascript. It assumes you are base 8 basically -
Dave Cousineau almost 7 yearswhat is this even doing?
DateTime dt1 = dt
does nothing.