【问题标题】:Datetime value not showing properly in jQuery data table日期时间值未在 jQuery 数据表中正确显示
【发布时间】:2018-11-23 13:12:05
【问题描述】:

我想获取数据表中的时间戳。如何在 jquery 数据表函数中格式化我的日期时间?

sql server 数据时间数据,如何编辑列,以便我无法在数据时间中获得正确的日期时间。我尝试过输入:'date-dd-mmm-yyyy',但它不起作用

 2018-11-23 07:49:35.073
     data type - date time

javascript

$(document).ready(function () {
                            $('#myTable').DataTable({
                                "ajax": {
                                    "url": "/Temperature/loaddata",
                                    "type": "GET",
                                    "datatype": "json"
                                },
                                "columns": [
                                        { "data": "id", "autoWidth": true },
                                        { "data": "updatedDate", "autoWidth": true, type: 'date-dd-mmm-yyyy', targets: 0 },
                                        { "data": "deviceid", "autoWidth": true },
                                        { "data": "devicename", "autoWidth": true },
                                        { "data": "temp", "autoWidth": true },
                                        { "data": "faht", "autoWidth": true }
                                ]
                            });
                        });

c#代码

public  ActionResult loaddata()
{ 
    using (smartpondEntities dc = new smartpondEntities())
    {
        var data = dc.Temperatures.OrderBy(a => a.id).ToList();
        return Json(new { data = data}, JsonRequestBehavior.AllowGet);
    }
}

json 样本数据

[{"id":1,"updatedDate":"2018-11-23T07:49:35.073","DeviceTime":null,"deviceid":1,"devicename":"aaaa","temp":28.50,"faht":87.90},{"id":2,"updatedDate":"2018-11-23T07:49:42.1","DeviceTime":null,"deviceid":1,"devicename":"aaaa","temp":28.50,"faht":87.90}]

输出

/Date(1542939575073)/

exp 操作

23 Nov 2018 13:10:10 

【问题讨论】:

  • 日期很好。这只是格式化字符串的问题。您需要在显示日期时将其格式化为预期的日期。

标签: javascript c# datatables


【解决方案1】:

我使用moment.js 作为日期时间(docs)。所以在你的渲染中:

{
    title: "Date",// name 
    render: function (data, type, row) {//data
        return moment(row.updatedDate).format('DD/MM/YYYY hh:mm:ss');
    }
}

【讨论】:

    【解决方案2】:

    您应该使用渲染函数来格式化/自定义您的数据

    例如:

    "columns": [
    { "data": "engine" },
    { "data": "browser" },
    {
      "data": "platform",
      "render": function ( data, type, row, meta ) {
                   return ConvertToDate(data);
                }
    }
    

    在 ConvertToDate(data) 函数中转换您的日期。

    function ConvertToDate(data){
         return data;//Converted date
    }
    

    【讨论】:

    • 错误提示 - “ConvertToDate 未定义”,我是否需要创建另一个函数
    • 你需要添加一个名为 ConvertToDate(date) 的函数
    猜你喜欢
    • 2019-12-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-05-16
    • 2017-05-14
    相关资源
    最近更新 更多