Click here to Skip to main content
15,897,291 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
See more:
i Have date format 21/01/2015 . I need to change this date to 1326067200000
Posted
Updated 24-Jan-17 9:39am

Try the code

C#
public class DateHandler : IHttpHandler
{

    public void ProcessRequest(HttpContext context)
    {
        context.Response.ContentType = "application/json";
        string action = context.Request.QueryString["jsonmode"];
        string json = null;

        if (!string.IsNullOrEmpty(action) && action == ".net")
        {
            // Creates date in .NET date format "\/Date(14123123132)\/"
            JavaScriptSerializer ser = new JavaScriptSerializer();
DateTime dt = DateTime.ParseExact("21/01/2015", "dd/MM/yyyy", CultureInfo.InvariantCulture);

            json = ser.Serialize(dt.Date);// pass your date
        }
        else
            // iso format: "2010-08-31T01:35:05.785Z"
            json = "\"" + DateTime.Now.ToUniversalTime().ToString("s") +
                   "Z" + "\"";

        context.Response.Write(json);
    }

    public bool IsReusable
    {
        get { return false; }
    }
}
 
Share this answer
 
v2
In Following code user is output which you want to send in JSON format:
System.Web.Script.Serialization.JavaScriptSerializer js = new System.Web.Script.Serialization.JavaScriptSerializer();

var data = js.Serialize(users).Replace("\"\\/Date(","").Replace(")\\/\"", "");

var output = js.DeserializeObject(data)

previous output:
{"name":"Amit Choudhary","dob":"/Date(1485256523000)/"}

new output:
{"name":"Amit Choudhary","dob":1485256523000}
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900