0

I am trying to display date in dd-MM-yyyy but the date i am getting is always in this format:

2016-08-08T16:17:40.643

I am using asp.net mvc and returning data in json format but displaying this date with angular js.

Here is the answer i am trying from the Link and i have combined the answer given by Perishable Dave and dav_i:

     public class JsonNetFilterAttribute : ActionFilterAttribute
        {
            public override void OnActionExecuted(ActionExecutedContext filterContext)
            {
                if (filterContext.Result is JsonResult == false)
                {
                    return;
                }

                filterContext.Result = new JsonNetResult(
                    (JsonResult)filterContext.Result);
            }

            private class JsonNetResult : JsonResult
            {
                private const string _dateFormat = "dd-MM-yyyy";
                public JsonNetResult(JsonResult jsonResult)
                {

                    this.ContentEncoding = jsonResult.ContentEncoding;
                    this.ContentType = jsonResult.ContentType;
                    this.Data = jsonResult.Data;
                    this.JsonRequestBehavior = jsonResult.JsonRequestBehavior;
                    this.MaxJsonLength = jsonResult.MaxJsonLength;
                    this.RecursionLimit = jsonResult.RecursionLimit;
                }

                public override void ExecuteResult(ControllerContext context)
                {
                    if (context == null)
                    {
                        throw new ArgumentNullException("context");
                    }

                    var isMethodGet = string.Equals(
                        context.HttpContext.Request.HttpMethod,
                        "GET",
                        StringComparison.OrdinalIgnoreCase);

                    if (this.JsonRequestBehavior == JsonRequestBehavior.DenyGet
                        && isMethodGet)
                    {
                        throw new InvalidOperationException(
                            "GET not allowed! Change JsonRequestBehavior to AllowGet.");
                    }

                    var response = context.HttpContext.Response;

                    response.ContentType = string.IsNullOrEmpty(this.ContentType)
                        ? "application/json"
                        : this.ContentType;

                    if (this.ContentEncoding != null)
                    {
                        response.ContentEncoding = this.ContentEncoding;
                    }

                    if (this.Data != null)
                    {
                        // Using Json.NET serializer
                        var isoConvert = new IsoDateTimeConverter();
                        isoConvert.DateTimeFormat = _dateFormat;
                        response.Write(JsonConvert.SerializeObject(this.Data));
                    }
                }
            }
        }

[JsonNetFilter]
public ActionResult GetJson()
{
    return Json(new { hello = new Date(2016-08-02 05:49:11.000) }, JsonRequestBehavior.AllowGet)
}

How to date in dd-MM-yyyy format??

Community
  • 1
  • 1
I Love Stackoverflow
  • 6,738
  • 20
  • 97
  • 216

1 Answers1

2

You're not passing your isoConvert variable to JsonConvert.SerializeObject(this.Data), so it's never used. You need to pass it to SerializeObject using an appropriate overload:

response.Write(JsonConvert.SerializeObject(this.Data, new [] { isoConvert } ));
dbc
  • 104,963
  • 20
  • 228
  • 340
  • You are really genius man.thanks alot.Do i need to add this in FilterConfig?? – I Love Stackoverflow Aug 29 '16 at 07:22
  • 1
    @Learning - Try following the instructions in [this answer](https://stackoverflow.com/questions/7109967/using-json-net-as-the-default-json-serializer-in-asp-net-mvc-3-is-it-possible/23544953#23544953). – dbc Aug 29 '16 at 07:26
  • But my question is why do i need to add in global.asax or in filterconfig as i would used it on some method only.i am little confused.can you please tell me if you know the answer – I Love Stackoverflow Aug 29 '16 at 07:29
  • 1
    @Learning - Not 100% sure. I *think* the idea of [that answer](https://stackoverflow.com/questions/7109967/using-json-net-as-the-default-json-serializer-in-asp-net-mvc-3-is-it-possible/23544953#23544953) is to make it so that you can add your `[JsonNetFilter]` to any method in any controller to have it use Json.NET to serialize the result. If you just want to use Json.NET in one specific method, see [this answer](https://stackoverflow.com/questions/7109967/using-json-net-as-the-default-json-serializer-in-asp-net-mvc-3-is-it-possible/29769644#29769644). – dbc Aug 29 '16 at 07:41
  • All right.Thank you so much for your kind help and explanation and please keep helping like this :) – I Love Stackoverflow Aug 29 '16 at 07:45