0

Ok fairly new to javascript entirely - I have the code below which is outputting the date in yyyy-MM-dd format. How would I go about converting this to UK format?

Apologies if this is a silly question but as I say I have kinda been thrown into the deep end and trying to work it out as I go.

dtl.zGetDueDt = function (row, cell, value, col, item) {

    if (item) {
        console.log("zGetDueDt -item", item);
        if (item.duedt === undefined) {
            item.duedt = "";

            DataService.get('api/ap/apsr/getbyrowid/' + dtl.aptransDataset[row].rowidApsr, { busy: true }, function (data) {
                if (data) {
                    console.log("APICall-data", data);
                    if (data.duedt !== null) {
                        dtl.aptransDataset[row].duedt = data.duedt.toString().substring(0, 10);
                        item.duedt = data.duedt.toString().substring(0, 10);
                        dtl.aptransGrid.updateRow(row);
                    }
                }
            }, row);
            return "";
        }
        else {
            return item.duedt;
        } // end of else return      
    } // end of if item
};  // end of function

});

  • What is "UK format"? Why don't any of the many existing answers about [*formatting Dates*](https://stackoverflow.com/search?q=%5Bjavascript%5D+how+to+format+a+date) (some with up to 50 answers) answer your question? – RobG Oct 16 '19 at 21:00

1 Answers1

1

You can use conversion getter methods, for example toLocaleString().

const date = new Date();
const output = document.querySelectorAll('output');

// Initial
output[0].textContent = date.toString();

//Converted
output[1].textContent = date.toLocaleString('en-GB');
<p>Initial: <output></output></p>
<p>Converted: <output></output></p>

See: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date#Conversion_getter

Itang Sanjana
  • 649
  • 5
  • 8