views:

1193

answers:

4

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

      }
     }
    }
}
A: 

+o-

jsonDateNow = String.Format("Date({0},{1},{2})", Date().Now.getYear(), Date().Now.getMonth() -1, Date().Now.getDay());
andres descalzo
The actually problem is that I need to serialize objects to JSON that contain dates and use those dates client-side.
x13
A: 

This example works

    JavaScriptSerializer serializer = new JavaScriptSerializer();

    DateTime dt = DateTime.Now;
    DateTime dt1 = dt;

    string jsonDateNow = serializer.Serialize(dt1);
andres descalzo
+2  A: 

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

value = new Date(parseInt(value.replace("/Date(", "").replace(")/",""), 10));
Chris S
Definitely the correct answer, nice post.
Allen
That 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.
x13
+1  A: 

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"));
    }
x13