使用Javascript转换JSON日期

时间:2017-02-06 15:03:02

标签: javascript jquery sql-server json datatables

在SQL Server数据库中,日期显示为 06-Feb-17 7:42:14 PM 。 但在Jquery DataTable中,此日期显示为 / Date(1486388669090)/

如果我想将日期完全显示为 06-Feb-17 7:42:14 PM 格式和“dd / mm / yy”格式??

这是我的代码:

$(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

                        }

                    ]
            });
 });

我们非常感谢任何适当的帮助。谢谢!

2 个答案:

答案 0 :(得分:1)

以“dd / mm / yyyy”格式显示Json日期格式:

    "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;

                            }

                  }

    ]

并且日期将显示为: 06/02/2017

完全按照“06-Feb-17 7:42:14 PM”显示Json日期格式:

"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;

                            }

                      }

        ]

并且日期将显示为: 06/02/2017 07:42:14 PM

答案 1 :(得分:0)

您需要将日期从纪元时间转换为您想要的时间格式。尝试Date Object的MDN页面。构造函数可以接受纪元时间,getYear,getMonth等函数将为您提供日期字符串的部分。