How Can Change jquery Datatable date column format dd-mm-yy

jewel 1,186 Reputation points
2023-07-22T05:51:54.9766667+00:00
In my asp.net core project the data in the datecolumn in jquery datatable looks like below.

I want to see the data in the following format.
16-Mar-2023

If someone could help. I would benefit. thanks in advance

 //HomeController
public JsonResult GetVenderRecord()         
{ var List = _context.tbl_Venders.ToList();              
return Json(List); 
}   

<div >
    <table class="display" id="MyDataTable">
        <thead>
            <tr>
             <th>
            Issue Date
           </th>
            </tr>
        </thead>
        <tbody>
        </tbody>
    </table>
</div>

<script type="text/javascript">
    $(document).ready(function () {
        GetVenderRecord();
    })
    var GetVenderRecord = function () {
        $.ajax({
            type: "Get",
            url: "/Home/GetVenderRecord",
            success: function (response) {
                BindDataTable(response);
            }
        })
    }
    var BindDataTable = function (response) {
 $("#MyDataTable").DataTable({
"aaData": response,
"aoColumns": [
{ "mData": "issueDate" }
 ]
   });}
</script>
Developer technologies ASP.NET ASP.NET Core
0 comments No comments
{count} votes

Accepted answer
  1. Anonymous
    2023-07-24T05:42:01.6733333+00:00

    Hi @jewel

    As Bruce said, you can use "render" property to format your column display, refer to the following code:

                var BindDataTable = function (response) {
                     $("#MyDataTable").DataTable({
                    "aaData": response,
                    "aoColumns": [
                        { "mData":"name" },
                        { "mData": "issueDate", 
                            "render": function (data) {
                                var date = new Date(data);
                                const monthNames = ["January", "February", "March", "April", "May", "June",
                                    "July", "August", "September", "October", "November", "December"
                                ];
    
                                var month = date.getMonth(); 
                                return date.getDate() + "-" + monthNames[parseInt(month.toString().length > 1 ? month : "0" + month)] + "-" + date.getFullYear();
                            }
                        }
                     ] 
                    });
                }
    

    Then the output as below:

    User's image

    Besides, you can also use render property with the moment.js, code as below:

        <link rel="stylesheet" href="https://cdn.datatables.net/1.13.5/css/jquery.dataTables.min.css"/>
        <script src="https://cdn.datatables.net/1.13.5/js/jquery.dataTables.min.js"></script>
        <script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.29.2/moment.min.js"> </script>
        <script src=""> </script>
        <script type="text/javascript">
                $(document).ready(function () {
                    GetVenderRecord();
                })
                var GetVenderRecord = function () {
                    $.ajax({
                        type: "Get",
                        url: "/Home/GetVenderRecord",
                        success: function (response) {
                            BindDataTable(response);
                        }
                    })
                }
                var BindDataTable = function (response) {
                     $("#MyDataTable").DataTable({
                    "aaData": response,
                    "aoColumns": [
                        { "mData":"name" },
                        { "mData": "issueDate",
                            "render": DataTable.render.datetime('DD-MMM-YYYY')
                        }
                     ] 
                    });
                }
        </script>
    

    Then the output as below:

    User's image


    If the answer is the right solution, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".

    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

    Best regards,

    Dillion

    0 comments No comments

1 additional answer

Sort by: Most helpful
  1. Bruce (SqlWork.com) 77,686 Reputation points Volunteer Moderator
    2023-07-22T16:40:36.9+00:00

    Use the column render function to format

    https://datatables.net/reference/option/columns.render#function

    1 person found this answer helpful.

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.