0

I am learning a bit of .NET and handlebars, need some help figuring out how to deal with JSON date parsing in this case:

Method in my controller

    public JsonResult stats()
    {
        IQueryable<EnrollmentDateGroup> data = from student in db.Students
             group student by student.EnrollmentDate into dateGroup
             select new EnrollmentDateGroup()
               {
                  EnrollmentDate = dateGroup.Key,
                  StudentCount = dateGroup.Count()
               };

        return Json(data.ToList());
    }

Template using handlebars

        <script id="mytemplate" type="text/x-handlebars-template">
        <h3 style="margin-top:20px">Student Body Statistics</h3>
        <table>
            <tr>
                <th>Enrollment Date</th>
                <th>Students</th>
            </tr>
            {{#each this}}
            <tr>
                <td>{{{EnrollmentDate}}}</td>
                <td>{{{StudentCount}}}</td>
            </tr>
            {{/each}}
        </table>
    </script>

JavaScript

        <script type="text/javascript">
        (function () {
            var templateHtml = $("#mytemplate").html();
            var sourceHtml = Handlebars.compile(templateHtml);

            $.post("@Url.Action("stats")", "", function (data) {
                $('#stu').html(sourceHtml(data));
                console.log(data);
            }, "json");

        })();
    </script>

and my result coming from DB into data is a list of /Date(1125547200000)/

How can I parse this date to real date?

M4N
  • 94,805
  • 45
  • 217
  • 260
mambo
  • 29
  • 5
  • I had the same issue, I found a way around it by introducing properties to get DateTime object properties Year, Month, Day, Hour Minute Seconds. Then I create my own JavaScript date object from JSON – progrAmmar Feb 01 '16 at 06:46
  • 4
    http://stackoverflow.com/questions/206384/format-a-microsoft-json-date – Andreas Feb 01 '16 at 06:46
  • Or you could use `EnrollmentDate = dateGroup.Key.ToString("d")` (or what ever format you want. –  Feb 01 '16 at 07:09

0 Answers0