Converting JSON Date using Javascript

2019-09-15 17:55发布

In SQL Server Database the date showing as 06-Feb-17 7:42:14 PM. But in Jquery DataTable this date is showing as /Date(1486388669090)/

What I have to do if I want to show the date exactly as 06-Feb-17 7:42:14 PM format and "dd/mm/yy" format??

Here is my code:

$(document).ready(function () {
            $('#myTable').DataTable({

                "ajax": {
                    "url": "/Employees/LoadData",
                    "type": "GET",
                    "datatype": "json"
                },
                "columns" : [
                        { "data": "EmployeeName", "autoWidth": true },
                        { "data": "Department", "autoWidth": true },
                        { "data": "Age", "autoWidth": true },
                        { "data": "Gender", "autoWidth": true },
                        {
                            "data": "CreatedOn",// This is my date

                        }

                    ]
            });
 });

Any appropriate help will be highly appreciated. Thanks!

2条回答
太酷不给撩
2楼-- · 2019-09-15 18:16

You need to convert the date from epoch time to the time format you want. Try the MDN page for the Date Object. The constructor can take in the epoch time and the functions like getYear, getMonth and so forth will give you the parts for your date string.

查看更多
Explosion°爆炸
3楼-- · 2019-09-15 18:19

To Display Json Date in "dd/mm/yyyy" Format:

    "columns" : [
                 { "data": "EmployeeName", "autoWidth": true },
                 { "data": "Department", "autoWidth": true },
                 { "data": "Age", "autoWidth": true },
                 { "data": "Gender", "autoWidth": true },
                 {
                   "data": "CreatedOn",
                   "render": function(data) {
                                var dateString = data.substr(6);
                                var currentTime = new Date(parseInt(dateString));
                                var month = currentTime.getMonth() + 1;
                                var day = currentTime.getDate();
                                var year = currentTime.getFullYear();

                                return (day.toString().length > 1 ? day : "0" + day) +
                                "/" +
                                (month.toString().length > 1 ? month : "0" + month) +
                                "/" +
                                year + " " + time;

                            }

                  }

    ]

And The date will will be displayed as: 06/02/2017

To Display Json Date exactly as "06-Feb-17 7:42:14 PM" Format:

"columns" : [
                     { "data": "EmployeeName", "autoWidth": true },
                     { "data": "Department", "autoWidth": true },
                     { "data": "Age", "autoWidth": true },
                     { "data": "Gender", "autoWidth": true },
                     {
                       "data": "CreatedOn",
                       "render": function(data) {
                                var dateString = data.substr(6);
                                var currentTime = new Date(parseInt(dateString));
                                var month = currentTime.getMonth() + 1;
                                var day = currentTime.getDate();
                                var year = currentTime.getFullYear();
                                var hour = currentTime.getHours();
                                var minute = currentTime.getMinutes();
                                var seconds = currentTime.getSeconds();

                                var localStandarHour = hour > 12 ? hour - 12 : hour;

                        var time = (localStandarHour.toString().length > 1 ? localStandarHour : "0" + localStandarHour) + ":" + (minute.toString().length > 1 ? minute : "0" + minute) + ":"
                            + (seconds.toString().length > 1 ? seconds : "0" + seconds);


                                if (hour > 12 ) {
                                    time = time + " PM";
                                } else {
                                    time = time + " AM";
                                }


                                return (day.toString().length > 1 ? day : "0" + day) +
                                "/" +
                                (month.toString().length > 1 ? month : "0" + month) +
                                "/" +
                                year + " " + time;

                            }

                      }

        ]

And The date will will be displayed as: 06/02/2017 07:42:14 PM

查看更多
登录 后发表回答