neethu
neethu

Reputation: 65

Date not displaying correctly in jquery

I need to display date from controller to view through jQuery in MVC4 razor.But it just displaying correctly.

Date is not displaying as it is passed from controller

To display date I used the below code:

$.each(data, function (index, el) {
                        console.log(el);
                        for (i = 0 ; i <el.length; i++) {
                            tr = $('<tr class="eachitem" />');
                            td = $('<td>' + el[i]["ID"] + '</td>').appendTo(tr);
                            td1 = $('<td>' + el[i]["Date"] + '</td>').appendTo(tr);
                            td1 = $('<td>' + el[i]["status"] + '</td>').appendTo(tr);

                            tr.appendTo('table');
                        }

                    });

Upvotes: 3

Views: 813

Answers (2)

Colin Bacon
Colin Bacon

Reputation: 15609

JavaScript library

I can recommend using Moment.js to parse, manipulate and display dates. It takes the pain out of something quite difficult in JavaScript

Upvotes: 0

Stasel
Stasel

Reputation: 1298

You need to parse the date string to a valid JavaScript date format. In order to do this use the following snippet from the answer in this question

var date = new Date(parseInt(value.replace("/Date(", "").replace(")/",""), 10));

After that, you will have a proper JavaScript date object which you can print in the format of your choice:

var stringDate = value.toLocaleString()
var stringDate = value.toISOString()

Upvotes: 1

Related Questions