Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Javascript serialization of DateTime in asp.net is not giving a javascript date object?

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);              }         }     } } 
like image 874
Your Friend Ken Avatar asked Aug 03 '09 21:08

Your Friend Ken


People also ask

Can you serialize DateTime?

For serializing, you can use the DateTime(Offset). ToString method in your converter write logic. This method allows you to write DateTime and DateTimeOffset values using any of the standard date and time formats, and the custom date and time formats.

What is object serialization in JavaScript?

Object serialization is the process of converting an object's state to a string from which it can later be restored. ECMAScript 5 provides native functions JSON.stringify() and JSON.parse() to serialize and restore JavaScript objects. These functions use the JSON data interchange format.

What does serialize to JavaScript?

The process whereby an object or data structure is translated into a format suitable for transfer over a network, or storage (e.g. in an array buffer or file format). In JavaScript, for example, you can serialize an object to a JSON string by calling the function JSON. stringify() .


2 Answers

This is a known limitation with JSON. This answer might help you, specifically:

value = new Date(parseInt(value.replace("/Date(", "").replace(")/",""), 10)); 
like image 106
Chris S Avatar answered Sep 20 '22 23:09

Chris S


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"));     } 
like image 24
Your Friend Ken Avatar answered Sep 22 '22 23:09

Your Friend Ken