c# datetime read in .js

c# datetime read in .js

su33161su33161 Posts: 7Questions: 5Answers: 0

I used moment.js to read the datetime format of c # in .js.
Below is the data table I use.

var table = $("#demoGrid").DataTable({
    buttons: [
        {
            extend: "create",
            editor: editor,
        },
        {
            extend: "edit",
            editor: editor,
        },
        {
            extend: "remove",
            editor: editor,
        },
    ],
    "columns": [
        {
            data: "Date",
            render: function (data, type, row) {
                if (type === "sort" || type === "type") {
                    return data;
                }
                return moment(data).format("YYYY-MM-DD HH:mm");
            }
        },
    ]
});

Now I want to change the data with the edit button.
However, the default value for DateTime is / Date (1528697651000) /.
I want to be YYYY-MM-DD HH:mm:ss in this format.

var editor = new $.fn.dataTable.Editor({
    table: "#demoGrid",
    idSrc: 'SN',
    fields:
        [
            {
                label: "Date:",
                name: "Date",
                type: "datetime",
                format: "YYYY-MM-DD HH:mm:ss",
                def: function () { return new Date(); },
            },
        ],
});

Thank you so much in advance for your help

This question has an accepted answers - jump to answer

Answers

  • allanallan Posts: 63,498Questions: 1Answers: 10,470 Site admin
    Answer ✓

    The problem here is that Editor doesn't have a pre-formatter function similar to DataTables. It will always operate on the value in the data set - in this case a .NET date string.

    Are you Ajax loading the data (it isn't shown above, but it might have been removed for brevity)? If so, then use xhr to get the JSON returned from the server, loop over it and update the date strings to be formatted by Moment.

    Alternatively, if it is DOM loaded (or even if it is Ajax loaded), you could update the server-side script to format the .NET date for you.

    Allan

This discussion has been closed.