Reputation: 1931
I am using jquery plugin - Datatable , to render the gridview with sorting, searching & pagination.
I am able to get it to work, except when i try to render the date. Without any render filter, my date shows in the datatable as
/Date(1489005452663)/
and the date is stored in the database as date and timestamp
2017-02-14 15:52:02.177
When i include the render for my date in jquery, the datatable doesnt populate anything and it shows blank table.
Here is the code I am using to populate my datatable:
$(document).ready(function() {
$.ajax({
url: 'ReportService.asmx/LoadReport',
method: 'post',
dataType: 'json',
success: function(data) {
$('#datatable').dataTable({
data: data,
columns: [
{ 'data': 'CMS_CONTRACT_NUMBER' },
{ 'data': 'submissionid' },
{ 'data': 'file1status' },
{ 'data': 'file2status' },
{
'data': 'datesubmitted',
'render': function (data)
{
var date = new Date(parseInt(data.substr(6)));
var month = date.getMonth() + 1;
return date.getFullYear() + "/" + month + "/" + date.getDate ;
// return data;
}
}
]
});
}
});
});
I am not sure what's causing the render inclusion not to populate any data.
Upvotes: 1
Views: 1128
Reputation: 1802
Use this in render
function:
return (moment(new Date(data)).isValid() ? moment(new Date(data)).format('MMMM Do, YYYY @ h:mm:ss a') : ' -- ');
Upvotes: 0
Reputation:
Make sure you call the getDate
method by including the parentheses:
return date.getFullYear() + "/" + month + "/" + date.getDate();
// ^
With the parentheses it should work:
var data = '/Date(1489005452663)/';
var date = new Date(parseInt(data.substr(6)));
var month = date.getMonth() + 1;
console.log(date.getFullYear() + "/" + month + "/" + date.getDate());
There must be something else happening that we can't see. Of course, you could also try a different method of parsing out the numbers:
var data = '/Date(1489005452663)/';
var date = new Date(+data.replace(/\D/ig,""));
var month = date.getMonth() + 1;
console.log(date.getFullYear() + "/" + month + "/" + date.getDate());
Upvotes: 2