James Peel
James Peel

Reputation: 179

Parsing date in Json Encoded array

I have built a MVC site and I am passing in my model to the view and attempting to use it in some javascript in the view using:

var records = @Html.Raw(Json.Encode(Model)); 

I am aiming on using these records in a custom calendar object that accepts an object array as a datasource. However, the datasource expects date variables to know where to put appointments etc. I've read that Json does not have any date format of it's own. So, all the dates that are returned from the encode are formatted like:

/Date(1462921200000)/

The object doesn't seem to accept this and nothing is shown on the view. In the posts that I've read they have stated that you can parse the date back into the correct format but this seems to be only on individual values.

My question is: is there an easy way to take the encoded object I've got and parse the dates into the correct format? Or would I have to loop through them in order to do it?

Thanks.

Upvotes: 0

Views: 499

Answers (3)

Alberto Chiesa
Alberto Chiesa

Reputation: 7350

Warning: This answer is opinionated. The opinionated part is: when you need something done with Json, use Json.Net.

namespace System.Web.MVC
{
  public static class HtmlHelperJsonExtensions
  {
    public string JsonEncode(this System.Web.MVC.HtmlHelper html, object o)
    {
      var jsonSettings = new Newtonsoft.Json.JsonSerializerSettings()
      {
        // Here you can apply a LOT of formatting, as a small example regarding dates:
        // 1 - formats dates as iso strings (probably what you want)
        DateFormatHandling = Newtonsoft.Json.DateFormatHandling.IsoDateFormat,
        // 2 - formats dates with Microsoft format (what you're experiencing)
        //DateFormatHandling = Newtonsoft.Json.DateFormatHandling.MicrosoftDateFormat,
        // 3 - formats date in a custom format as for DateTime.ToString(string) overload
        //DateFormatString = "yyyy-MM-dd"
        Formatting = Indented
      };

      var json = Newtonsoft.Json.JsonConvert.SerializeObject(o, jsonSettings);

      return html.Raw(json);
    }
  }
}

Usage:

var records = @Html.JsonEncode(Model);

Refer to the excellent Json.Net documentation here for insights.

Upvotes: 1

Álvaro González
Álvaro González

Reputation: 146450

Disclaimer: question was not identified as ASP.NET when I composed this answer. I presume the OP is asking for some feature provided by his yet to be disclosed framework. I keep the answer because I think it's valid for manual decoding in vanilla JavaScript.


The format can be parsed with a simple regular expression

var asText = "Date(1462921200000)";
var asDate = null;
var parsed = asText.match(/^Date\((\d+)\)$/);
if (parsed!==null) {
	asDate = new Date(+parsed[1]);
}
console.log(asDate);

Upvotes: 0

Darion Badlydone
Darion Badlydone

Reputation: 947

Asp.Net MVC application return the dates in the ISO-8601 format by default. You can convert the value in the client side with a function like this:

function ConvertDate(dateString) {
    return new Date(parseInt(dateString.replace("/Date(", "").replace(")/", ""), 10));
  }

Or you can use a library like momentjs (https://momentjs.com/docs/#/parsing/asp-net-json-date/):

moment("/Date(1198908717056-0700)/"); // 2007-12-28T23:11:57.056-07:00

Upvotes: 0

Related Questions