0

I'm having an issue with a REST call that's giving me some headaches. This is the code I'm using:

function LoadAIP(state)
{
var call = $.ajax({
url: "https://xxxx/xxxx/_vti_bin/listdata.svc/AIPList?  
$select=ROCode,AIP,NotificationDate&$filter=(ROCode eq '"+state+"')&$top=5000",

type: "GET",
dataType: "json",
headers: {
Accept: "application/json;odata=verbose"
}
});

call.done(function (data,textStatus, jqXHR){
$('#example').dataTable( {
"bJQueryUI": true,
"sPaginationType": "full_numbers",
 "aaData": data.d.results,
"aoColumns": [
{ "mData": "ROCode" },
{ "mData": "AIP" },
{ "mData": "NotificationDate" }],"bRetrieve": true,

"sDom": '<"H"Tfr>t<"F"ip>',
"oTableTools": {
"aButtons": [ "xls"], 
"sSwfPath": ../js/datatables/TableTools/media/swf/copy_csv_xls_pdf.swf",
"bFilter": true}
} );
});
call.fail(function (jqXHR,textStatus,errorThrown){
alert("Error retrieving Tasks: " + jqXHR.responseText);
});
}

It works except with one of the columns (NotificationDate). This field, as the name says it it a date field. When I make the call instead of showing a date, as would expect, it shows something else. For example, one of the rows it should show 4/9/2014, but instead shows this: /Date(1397001600000)/. I had a similar problem some time ago with XSLT and I used the ddwrt:DateTimeTick function to fix it in a Data view web part, but in this case I don't know what to do. Thanks!

2

According to MSDN DateTime object:

represented in JSON as "/Date(number of ticks)/". The number of ticks is a positive or negative long value that indicates the number of ticks (milliseconds) that have elapsed since midnight 01 January, 1970 UTC. The maximum supported date value is MaxValue (12/31/9999 11:59:59 PM) and the minimum supported date value is MinValue (1/1/0001 12:00:00 AM).

To convert JSON DateTime object into DateTime the following function could be used:

function dateFromJSON(json)
{
    return new Date(parseInt(json.replace(/\/Date\((\d+)\)\//gi, "$1")));
}

Usage:

var createdDate = dateFromJSON(taskItem.Created);

Please follow this answer for another options.

Or leverage library for parsing, validating, manipulating, and formatting dates like Moment.js:

var createdDate = moment(taskItem.Created); //convert JSON DateTime 
console.log(createdDate.toString()); 
  • As I mentioned in my provious comments. I used moment.js. I added another option to datatables(): "fnInitComplete":function() { $('#example thead tr th:eq(2)').each(function(i) { var theCells = $(this).parents('#example') .find('tbody tr td:nth-child(3)');//The column if($(this).html()!=null) { theCells.html(moment(this).format('L')); } . But it shows all the dates as today's date. Thanks! – cubanGuy Apr 25 '14 at 17:43
1

I recommend the moment.js library for handling date processing and formatting in javascript.

  • I went ahead and used moment.js. It partically works. The problem is that not is showing me all dates as "today's date": 4/25/2014. I'm using: moment(string).format('L'). Any ideas? – cubanGuy Apr 25 '14 at 17:37
0

I found out that all that is needed is to add this to the column containing the Jason Date:

function LoadAIP(state) {
var call = $.ajax({
    url: "https://xxxx/xxxx/_vti_bin/listdata.svc/AIPList?  
$select=ROCode,AIP,NotificationDate&$filter=(ROCode eq '" + state + "')&$top=5000",

    type: "GET",
    dataType: "json",
    headers: {
        Accept: "application/json;odata=verbose"
    }
});

call.done(function (data, textStatus, jqXHR) {
    $('#example').dataTable({
        "bJQueryUI": true,
            "sPaginationType": "full_numbers",
            "aaData": data.d.results,
            "aoColumns": [{
            "mData": "ROCode"
        }, {
            "mData": "AIP"
        }, {
            "mData": "NotificationDate",
            "fnRender": function (obj, val) {
                var dx = new Date(parseInt(val.substr(6)));
                var dd = dx.getDate();
                var mm = dx.getMonth() + 1;
                var yy = dx.getFullYear();

                if (dd <= 9) {
                    dd = "0" + dd;
                }
                if (mm <= 9) {
                    mm = "0" + mm;
                }
                return dd + "/" + mm + "/" + yy;
            }
        }]
    }], "bRetrieve": true,

        "sDom": '<"H"Tfr>t<"F"ip>',
        "oTableTools": {
        "aButtons": ["xls"],
            "sSwfPath": .. / js / datatables / TableTools / media / swf / copy_csv_xls_pdf.swf ",

"bFilter ": true}
} );
});
call.fail(function (jqXHR,textStatus,errorThrown){
alert("
        Error retrieving Tasks: " + jqXHR.responseText);
});
}
0
I used in the rest call

**Date Variable is: Created **

var call = $.ajax({
            url: "../_api/Web/Lists/GetByTitle('NewUser')/items?$filter=(Created%20ge%20datetime'"+FromDate+"')%20and%20(Created%20le%20datetime'"+ToDate+"' and Title eq '"+epf+"' )&$top=5000",
            type: "GET",
            dataType: "json",
            headers: {
                Accept: "application/json;odata=verbose"
            }

            });

  call.done(function (data,textStatus, jqXHR){
        $('#example').dataTable({
            "bDestroy": true,
            "bProcessing": true,
            "aaData": data.d.results,
            "aLengthMenu" : [
             [50,100],
             [50,100]
            ],
             dom: 'Bfrtip',
            buttons: [
                'copy', 'csv', 'excel'
            ],

            "aoColumnDefs": [{ "bVisible": false  }],
            "aoColumns": [
                { "mData": "ID" },
                { "mData": "Title" },
                { "mData": "EmployeeName" },
                { "mData": "Department1" },
                { "mData": "ServicingAt" },
                { "mData": "TestField" }, 
                { "mData": "BranchCode" },   
                { "mData": "Created" ,"render": function (data, type, row) {
        data = moment(data).format('DD MMM YYYY');
        return data;
    }

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.