0

I want to convert a JSON formatted Date to a formatted string.

I got this date format I want to convert to a .Net DateT : "/Date(1480593300000)/"

enter image description here

public JsonResult DateFormat(string date)
{
    DateTime temp = new DateTime(int.Parse(date.Substring(6, 13)));
    string res = temp.ToString("g", Thread.CurrentThread.CurrentCulture);
    return Json(res, JsonRequestBehavior.AllowGet);
}

This is how I tried to do it, I get 02/01/0001 17:07.

So my question is, is there a better, more efficient way to do it ?

Also, is there a way in C# to display this specific format, depending on the culture : fr-FR : 31/12 17:07 en-US : 12/31 5:07pm ? I could do it manually but an equivalent to the

DateTime.ToString("g", IFormatProvider) 

for this format would be perfect.

Thomas Sauvajon
  • 1,660
  • 2
  • 13
  • 26
  • That's not a JSON Date. JSON uses the ISO8601 format. That's an old-style date, used before there *was* a defacto standard for dates. There are a *lot* of duplicate questions about this. Json.NET can handle both of the formats, in fact, that was its default format before v3 (I think) – Panagiotis Kanavos Jan 02 '17 at 12:16
  • Where does this string come from? Is the input string an actual Json string? Why don't you use JSon.NET to deserialize the entire string? – Panagiotis Kanavos Jan 02 '17 at 12:18
  • This string comes from an object I serialize using System.Web.Mvc.Controller.Json() ; I tried to use a reverse method without success. – Thomas Sauvajon Jan 02 '17 at 12:28
  • Which MVC version? Older versions did return the old Ajax format but later versions return ISO8601 dates. Perhaps you can add the `[JsonConverter(typeof(IsoDateTimeConverter))]` attribute to the DateTime property as shown [here](http://stackoverflow.com/questions/10527001/asp-net-mvc-controller-json-datetime-serialization-vs-newtonsoft-json-datetime-s). – Panagiotis Kanavos Jan 02 '17 at 12:39
  • I am using MVC 5.2.3.0. Thanks for the link I'll try that ! – Thomas Sauvajon Jan 02 '17 at 12:52

0 Answers0