0

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 Answers 2

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

Sign up to request clarification or add additional context in comments.

Comments

0

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.