0

I am a Beginner at JQuery(Json).I am not sure if it is possible or not to append DateTime to the Json table.I have a dropdown list.By selecting on a item its automatically load some data on table. For doing so I am trying a json function like this:

<script>

    $(document).ready(function () {

        $("#departmentId").change(function () {

            var deptId = $("#departmentId").val();
            //$("#courseId").empty();
            var json = { departmentId: deptId };
            $("#table").empty();
            $("#table").append('<table class="table"><tr><th>Course Code</th><th>Name</th><th>Schedule Info</th></tr>');
            $.ajax({
                type: "POST",
                url: '@Url.Action("ViewAllScheduleByDept", "ClassSchedule")',
                contentType: "application/json; charset=utf-8",
                data: JSON.stringify(json),
                success: function (data) {

                    $.each(data, function (key, value) {

                        $("#table").append('<tr><td>' + value.Code + '</td><td>' + value.Name + '</td><td>' + value.RoomName + ',' + ***value.StartTime.toDateString() +*** ' </td></tr>');

                    });
                    $('#table').append('</table>');
                }
            });
        });
    });
</script> 

It Displays Code,Name and RoomName perfectly but if I add StartTime to append along with these three item, It shows nothing. Is there any possible way to convert DateTime right on the Json table-append?. If not then how can I do show date time along with Code,Name and room name on MVC Razor view.

Here is my Controller:

[HttpPost]
    public JsonResult ViewAllScheduleByDept(int departmentId)
    {
        var schedules = GetAllSchedule();
        var scheduleList = schedules.Where(a => a.DepartmentId == departmentId).ToList();
        return Json(scheduleList, JsonRequestBehavior.AllowGet);

    }

    private List<ViewSchedule> GetAllSchedule()
    {
        List<ViewSchedule> allViewSchedules = scheduleManager.ViewAllSchedule();
        return allViewSchedules;

    }
}

And Model:

public class ViewSchedule
{
    public int DepartmentId { get; set; }
    public string Code { get; set; }
    public string Name { get; set; }
    public string RoomName { get; set; }
    public DateTime StartTime { get; set; }


}
4
  • 1
    Try without toDateString call on the StartTime property Commented Aug 26, 2016 at 19:11
  • Try with : + value.StartTime.toString() + Commented Aug 26, 2016 at 19:23
  • 1
    $('#table').append('</table>') doesn't make sense. Commented Aug 26, 2016 at 19:32
  • Just to be clear on naming: there's no such thing as a "JSON table". JSON is a string, that is then converted to an object. The data argument to your success function is that converted object, not JSON. The table is HTML that you're creating from the object. Commented Aug 26, 2016 at 20:12

1 Answer 1

1

The json date your endpoint returned will be some thing like this. See the value of StartTime property.

[{
   "Code":"SomeCode",
   "Name":"Some Name",
   "StartTime" :"/Date(1472239122527)/"
}]

That is the Epoch representation of the DateTime value you had. The json serializer converted the date time value to it's corresponding unix epoch time (the number of seconds that have elapsed since 00:00:00 Coordinated Universal Time (UTC), Thursday, 1 January 1970).

But in your code you are trying to call the toDateString() method on that. toDateString() should be called on a Date object.

So convert this number to a valid Date object first and then call the toDateString method on that.

$.each(data, function (key, value) {

   var dateFormatted = new Date(parseInt(value.StartTime.substr(6))).toDateString();
   $("#table").append('<tr><td>' + value.Code
                   + '</td><td>' + value.Name 
                   + '</td><td>' + value.RoomName
                   + ',' + dateFormatted + ' </td></tr>');

});
Sign up to request clarification or add additional context in comments.

2 Comments

Thanks, It works.If I want just Time not the date then where i need to change @Shyju
You can extract the time info from the Date object. Take a look at the mdn documentation on Date. developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/…

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.