0

I am querying my database and returning a jsonresult, and using that to populate a grid, and I see that my date being returned is /Date(1507477743793)/. I have looked around and have seen solutions when it comes to converting one variable but not when it comes to an array of objects (I think thats right)

EDIT

Sample data

FileAReportID:1
ReportAddress:"1 Main Street West"
ReportDate:"/Date(1507477743793)/"
ReporterName:"Beetle Bailey"
Chris
  • 2,953
  • 10
  • 48
  • 118

2 Answers2

0

This number 1507477743793 is the timestamp from your server. to conver this in other format just try this:

let t = (new Date(1507477743793)).toUTCString();
console.log(t)

// output
and you get: 2017/12/6 05:32:30pm
George C.
  • 6,574
  • 12
  • 55
  • 80
0

You can create custom jsonresult

public class CustomJsonResult : JsonResult
{
    private const string _dateFormat = "yyyy-MM-dd HH:mm:ss";

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

        HttpResponseBase response = context.HttpContext.Response;

        if (!String.IsNullOrEmpty(ContentType))
        {
            response.ContentType = ContentType;
        }
        else
        {
            response.ContentType = "application/json";
        }
        if (ContentEncoding != null)
        {
            response.ContentEncoding = ContentEncoding;
        }
        if (Data != null)
        {
            // Using Json.NET serializer
            var isoConvert = new IsoDateTimeConverter();
            isoConvert.DateTimeFormat = _dateFormat;
            response.Write(JsonConvert.SerializeObject(Data, isoConvert));
        }
    }
}

Usage Example:

[HttpGet]
public ActionResult Index() {
    return new CustomJsonResult { Data = new {  }  };
}

You can also create custom MediaTypeFormatter formatter and during serialization you can change the format of datetime.

Update

Json.Net supports multiple ways of formatting a date, if your consuming it from an ajax call you may want to look into JavaScriptDateTimeConverter.

Serializing Dates in JSON

santosh singh
  • 27,666
  • 26
  • 83
  • 129
  • I seen an example like this but I was kind of hoping to do this in jquery when the data is returned successfully from the ajax call – Chris Oct 08 '17 at 17:32