2

I have a DataTable that's populated by axios get request data, and it includes times that used to be properly converted with help from moment.js. My code was working when I was using a local JSON file, but ever since switching to a url I've had to do a lot of code rewriting.

In the console the data comes up as date-times with the format YYYY-DD-MMThh:mm:ss, but when they're rendered to the browser they display as 01/01/1970 automatically. I figured that it might have to do with the code I was using earlier.

How can I rewrite my function so that it does render YYYY-DD-MMThh:mm:ss appropriately, preferably to MM/DD/YYYY?

JS snippet:

loadAdmData(response) {
        let admissText = response.map(function(val) {
            return {
                "Date of Adm": val.DateofAdm, 
                "Expires": val.Expires
            }
        })

    $('#admissions-table').DataTable({
        columns: [
        ... // ---- irrelevant data
        ...
        ...
            { data: "Date of Adm" },
            { data: "Expires" }
        ],
        columnDefs: [
            {"type":"unix","targets":3,"render": function(data) {
                return moment.utc(data, "x").format('MM/DD/YYYY')
            }} // targets must be plural
        ],
...etc

Object snippet (console):

DateofAdm"1994-03-02T05:00:00Z"
Bodrov
  • 840
  • 1
  • 15
  • 29
  • 1
    Possible duplicate of [Convert Date from one format to another format in JavaScript](https://stackoverflow.com/questions/26500694/convert-date-from-one-format-to-another-format-in-javascript) – Heretic Monkey Feb 28 '19 at 19:56
  • Have you tried `moment(data, 'YYYY-DD-MMThh:mm:ss').format('MM/DD/YYYY')`? – User863 Mar 01 '19 at 05:32

0 Answers0