0

I have a local JSON file that's being rendered into a DataTable. Everything in the table is fine except the dates are rendered like this: /Date(1350028800000)/ (10/12/2012).

I looked up this thread about converting the data but based on how information is rendered into a DataTable I'm not sure how I would proceed. Any thoughts on this one?

JS snippet:

import $ from 'jquery';
import admissData from '../JSON/atty-admiss.json';
import DataTable from 'datatables.net';

function loadAdmiss() {
    let admissText = admissData.d.results.map(function(val) {
        return {
            "Status": val.admissionstatus,
            // "Date of Admission": val.dateofadmission,
            "Expires": val.expires,
            "Classification": val.classification
        }
    })
    let admissDate = admissData.d.results.map(function(val) {
        let newDate = new Date(admissDate) { //// ?
            return val.dateofadmission
        }
    })

    $('#admissions-table').DataTable({
        columns: [
            { data: "Status" },
            { data: "Date of Admission" }, // ------ DT's method of injecting relevant JSON data into each column
            { data: "Expires" },
            { data: "Classification" }
        ],
        data: admissText, // ---------- how the JSON data enters DT
        lengthChange: false,
        paging: true,
        pagingType: "full_numbers"
    });

}

loadAdmiss();
Bodrov
  • 840
  • 1
  • 15
  • 29
  • What is your actual question? The referenced post shows how to convert an MS JSON date to an ECMAScript Date. Are you asking how to apply that to your code? – RobG Feb 14 '19 at 23:38

1 Answers1

0

Does this help ?

var admissData = {
    d: {
        results: [
            '/Date(1350028800000)/',
            '/Date(1550185211515)/',
        ]
    }
};


var parseMicorsoftFormat = function(weirdDateFormat) {
    if(typeof weirdDateFormat === 'string') { //lets assume we have a string to work with 
        var extractedData = !!weirdDateFormat.match(/\d+/g) ? weirdDateFormat.match(/\d+/g)[0] : null;
        if(extractedData) {
            return new Date(parseInt(extractedData)).toLocaleString().match(/[^,]*/)[0];
        }else {
            return "No numbers found"
        }
    }else {
        return "Wrong Format"
    }
};

admissData.d.results = admissData.d.results.map(date => parseMicorsoftFormat(date));

  • Welcome to Stack Overflow! Thank you for this code snippet, which might provide some limited, immediate help. A [proper explanation would greatly improve its long-term value](//meta.stackexchange.com/q/114762/206345) by showing _why_ this is a good solution to the problem, and would make it more useful to future readers with other, similar questions. Please [edit] your answer to add some explanation, including the assumptions you've made. – Chris Feb 15 '19 at 00:12