Tri Setia

Tri Setia

  • 1.3k
  • 464
  • 24.2k

Formatted dates (client-side) using Jquery in ASP.NET Mvc .NET CORE

Apr 12 2021 3:50 PM
hi guys,, I'm try to render date format using jquery dataTables in view for get result date 12/04/2021 but I'm always get data date 12/04/2021 00.00.00. I'm want to 00.00.00 behind date disappearing when data rendering in view.
 
I'm get references link from here https://editor.datatables.net/examples/dates/formatting-client.html.
 
maybe any other idea, I will hear it. any help could be appreciated. 
 
this is the Index.cshtml
  1. @{    
  2.     @model System.Data.DataTable    
  3.     ViewData["Title"] = "Data Pasien";    
  4.     Layout = "~/Views/Shared/AdminLayout.cshtml";    
  5. }    
  6.     
  7. <link href="~/plugins/fontawesome-free/css/all.css" rel="stylesheet" />    
  8. <link href="~/plugins/sweetalert2/sweetalert2.min.css" rel="stylesheet" />    
  9. <script src="~/plugins/sweetalert2/sweetalert2.min.js"></script>    
  10. @if (TempData["pesan"] != null)    
  11. {    
  12.     <script type="text/javascript">    
  13.             window.onload = function () {    
  14.                 swal.fire("Good job!"'@TempData["pesan"]'"success");    
  15.             };    
  16.     </script>    
  17. }    
  18. <!-- Main content -->    
  19. <section class="content">    
  20.     <div class="container-fluid">    
  21.         <div>    
  22.             <a asp-action="TambahData"><i class="fa fa-plus-square btn btn-success"> Tambah Data Pasien </i></a>    
  23.         </div>    
  24.         <hr />    
  25.         <table class="table table-bordered table-responsive-lg table-hover" width="100%" id="myTable">    
  26.             <thead class="thead-dark text-center">    
  27.                 <tr>    
  28.                     <th>    
  29.                         NIK KTP    
  30.                     </th>    
  31.                     <th>    
  32.                         Nama Pasien    
  33.                     </th>    
  34.                     <th>    
  35.                         Alamat    
  36.                     </th>    
  37.                     <th>    
  38.                         Jenis Kelamin    
  39.                     </th>    
  40.                     <th>    
  41.                         Tanggal Lahir    
  42.                     </th>    
  43.                     <th>    
  44.                         Status    
  45.                     </th>    
  46.                     <th>    
  47.                         Action    
  48.                     </th>    
  49.                 </tr>    
  50.             </thead>    
  51.             <tbody class="text-center">    
  52.                 @for (int i = 0; i < Model.Rows.Count; i++)    
  53.                 {    
  54.                     <tr>    
  55.                         <td>    
  56.                             @Model.Rows[i]["Nik"]    
  57.                         </td>    
  58.                         <td>    
  59.                             @Model.Rows[i]["Nama_Pasien"]    
  60.                         </td>    
  61.                         <td>    
  62.                             @Model.Rows[i]["Alamat"]    
  63.                         </td>    
  64.                         <td>    
  65.                             @Model.Rows[i]["Jenis_Kelamin"]    
  66.                         </td>    
  67.                         <td>    
  68.                             @*in this line when render datetime in view I want to get result 12/04/2021*@    
  69.                             @Model.Rows[i]["Tanggal_Lahir"]    
  70.                         </td>    
  71.                         <td>    
  72.                             @Model.Rows[i]["Status"]    
  73.                         </td>    
  74.                         <td>    
  75.                             <a asp-action="UpdateData" asp-route-id="@Model.Rows[i]["Id_Pasien"]"><i class="fa fa-edit btn btn-sm btn btn-info"> Edit</i> </a> |    
  76.                             <a asp-action="Delete" asp-route-id="@Model.Rows[i]["Id_Pasien"]"><i class="fa fa-trash btn btn-sm btn btn-danger"> Delete</i> </a>    
  77.                         </td>    
  78.                     </tr>    
  79.                 }    
  80.             </tbody>    
  81.         </table>    
  82.     </div>    
  83.     
  84.     <!-- jQuery -->    
  85.     
  86.     <script src="~/plugins/jquery/jquery-3.5.1.js"></script>    
  87.     <link href="~/plugins/datatables/jquery.dataTables.min.css" rel="stylesheet" />    
  88.     <script src="~/plugins/datatables/jquery.dataTables.js"></script>    
  89.     <script src="~/plugins/moment/moment.min.js"></script>    
  90.     <script src="~/plugins/jquery-dateTime/dataTables.dateTime.min.js"></script>    
  91.     
  92.     <script type="text/javascript" language="javascript">    
  93.         $(document).ready(function () {    
  94.             $("#myTable").dataTable(    
  95.                 {    
  96.                     bLengthChange: true,    
  97.                     lengthMenu: [[10, 25, 50, 100], [10, 25, 50, 100]],    
  98.                     bFilter: true,    
  99.                     bSort: true,    
  100.                     bPaginate: true,    
  101.                   
  102.                     //For render datetime format to get result 12/04/2021    
  103.                     type: 'datetime',    
  104.                     def: function () { return new Date(); },    
  105.                     displayFormat: 'M/D/YYYY',    
  106.                     wireFormat: 'YYYY-MM-DD',    
  107.                     fieldInfo: 'US style m/d/y format',    
  108.                     keyInput: false,    
  109.                     render: $.fn.dataTable.render.moment('D/M/YYYY')    
  110.                 })    
  111.         })    
  112.     </script>    
  113. </section> 

Answers (3)