datatable time is different from database time

I am trying to show database time in the datatable but issue is show the current time

see database time

enter image description here

see datatable time

enter image description here

.cshtml

$.ajax({  
                url: strUrl,  
                type: 'POST',  
                success: function (result) {  
                      
                    console.log("inside the success method")  
                    $('#').DataTable({  
                        data: result,  
                        columns: [  
                            { data: "StartFromTime" },  
                            { data: "EndToTime" },  
                             
                        ],  
  
                        "aoColumnDefs": [  
                              {  
                                  "aTargets": [0],  
                                  //title: "StartFromTime",  
                                  "mRender": function (row) {//data  
                                      //console.log("inside the date and time:" + data + ":" + type);  
                                      //console.log(moment(row.StartFromTime).format('DD/MM/YYYY hh:mm:ss'));  
                                      //console.log("StartFromTime:" +moment(row.StartFromTime).format('hh:mm:ss'));  
                                      //console.log("innnn");  
                                      //console.log(row);  
                                      ////console.log(data);  
                                      //console.log(type);  
                                      //console.log(moment(row).format('hh:mm:ss'));  
                                      //var date = new Date(parseInt(data.substr(6)));  
                                      //console.log("date:" + date);  
                                      //return date.getDate();  
                                      return moment(row.StartFromTime).format('hh:mm:ss');  
  
                                  }  
                              },  
                              {  
                                  "aTargets": [1],  
                                 
                                  "mRender": function (row) {//data  
                                      return moment(row.EndToTime).format('hh:mm:ss');  
  
                                  }  
                              },  

model

public class TimeList  
{  
    public int Id { get; set; }  
    public DateTime StartFromTime { get; set; }  
    public DateTime EndToTime { get; set; }  

what I am trying:

below code also not work:

"mRender": function(row){    
var aDate= new Date(row);  
var dateString = '';  
  
var h = aDate.getHours();  
var m = aDate.getMinutes();  
var s = aDate.getSeconds();  
  
if (h < 10) h = '0' + h;  
if (m < 10) m = '0' + m;  
if (s < 10) s = '0' + s;  
  
dateString = h + ':' + m + ':' + s;  
return dateString;  
    }   

below code also not work:

var aDate = new Date(row.EnquiryFromTime);
console.log("gettime:" + aDate);
return aDate.getTime();

how to show the database time in datatable?

problem is moment() is built in jquery function that is the reason show the current time I want to show database time

I am reviewing continuously this moment.js file but not get any idea

https://momentjs.com/

I want to show database time in datatable?

help I am trying and trying but not work which place need to correction

1 answer

  • answered 2020-11-28 14:49 Ardy Setiawan

    I think your problem is timezone. your database timezone might be different with your browser timezone (your datatable works on browser timezone).

    first, take a look on ajax request, open your chrome console tab network, choose xhr and then reload the page. you will see your datatable requesting data with ajax, take a look the response.

    if the response dates are correct, just give the datatable column type as string (https://datatables.net/reference/option/columns.type), so you will see it as is. it will help you at the moment, but i guess it will get wrong sorting.

    the right way for your problem is get to know your database and your browser timezone (the OS timezone) and then adjust it with moment utc offset (https://momentjs.com/docs/#/manipulating/utc-offset/) to add or deduct the minutes (ie. utc +08:00 so 8 * 60 minutes = 480 as the utcOffset parameter)