Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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');
                        }

                    });
like image 279
neethu Avatar asked Nov 19 '25 18:11

neethu


1 Answers

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()
like image 159
Stasel Avatar answered Nov 22 '25 10:11

Stasel



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!