DataTables 从数据库中呈现错误的 DateTime 值

Posted

技术标签:

【中文标题】DataTables 从数据库中呈现错误的 DateTime 值【英文标题】:DataTables render wrong DateTime value from database 【发布时间】:2019-09-24 10:52:49 【问题描述】:

我在 asp.net mvc 项目中使用数据表插件,在绑定数据时它呈现错误的日期时间值

数据库中的实际日期时间值为 05-May-19 2:43:33 PM ,运行时显示的值为 /Date(1557060213477)/

它显示数据库中存在的日期时间值的预期结果

这是我的模型

 public int Id  get; set; 
 [Required]
 public string Name  get; set; 
 Display(Name ="Upload File")]
 public string FileUrl  get; set; 
 public DateTime AddedOn  get; set; 

这是我的看法

<table id="DDR" class="table table-striped table-bordered dt-responsive nowrap">
    <thead>
        <tr>
            <th>Id</th>
            <th>Name</th>
            <th>Uploaded File</th>
            <th>Uploaded At</th>
            <th>Edit</th>
            <th>Delete</th>
        </tr>
    </thead>
</table>
@section scripts 
    <link href="https://cdn.datatables.net/1.10.19/css/dataTables.bootstrap4.min.css" rel="stylesheet" />
    <script src="//cdn.datatables.net/1.10.19/js/jquery.dataTables.min.js"></script>
    <script src="https://cdn.datatables.net/1.10.19/js/dataTables.bootstrap4.min.js"></script>
    <script>
        $(document).ready(function () 
            debugger;
            $("#DDR").DataTable(
                "ajax": 
                    "url": "/DomesticDocRepositories/GetData",
                    "type": "GET",
                    "datatype": "json"
                ,
                "columns": [
                    
                        "data": "Id",
                        render: function (data, type) 
                            return '<a href="/DomesticDocRepositories/Details/' + data + '">'+data+"</a>";
                        
                    ,

                     "data": "Name" ,
                    
                        data: "FileUrl",
                        render: function (data, type) 
                            return "<a href='/DomesticFiles/" + data + " 'target='_blank''>" + data + "</a>";
                        
                    ,
                    
                        "data": "AddedOn",
                        render: function (data, type) 
                            return '<span "title='+ data +"'>" + data +"</span>";
                        
                    ,
                    
                        "render": function (data, type, full, meta)  return '<a class="btn btn-info" href="/DomesticDocRepositories/Edit/' + full.Id + '">Edit</a>'; 
                    ,
                    
                        data: null, render: function (data, type, row) 
                            return '<a class="btn btn-danger" href="/DomesticDocRepositories/Delete/' + row.Id + '">Delete</a>';
                        
                    ,
                ]
            );
        );
    </script>
    

【问题讨论】:

【参考方案1】:

使用moment.js 表示日期时间(docs)。在您的渲染中,您应该在渲染中使用:


    render: function (data, type, row) //data
        return moment(row.AddedOn ).format('DD/MM/YYYY hh:mm:ss');//or another format you wantt.
    

【讨论】:

Uncaught ReferenceError: moment is not defined @hassan 对不起,我忘了写moment.js,我编辑我的答案。

以上是关于DataTables 从数据库中呈现错误的 DateTime 值的主要内容,如果未能解决你的问题,请参考以下文章

DataTables 警告 - 请求第 0 行第 0 列的未知参数“0”

如何使用 rmarkdown 在 pdf 中呈现 DT::datatables?

在 jquery datatables.net 中呈现布尔数据列的最佳方法

jQuery DataTables 呈现列数据

请教jquery一个页面渲染两个datatables总是出现其中一个table加载不成功导致

如何更改 DataTables 中排序箭头的颜色