Converting JSON Date using Javascript

被刻印的时光 ゝ 提交于 2019-12-25 09:27:54

问题


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!


回答1:


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




回答2:


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.



来源:https://stackoverflow.com/questions/42070895/converting-json-date-using-javascript

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!