jQuery DataTables 如何只显示日期而不是日期时间

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/6654864/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-26 21:11:19  来源:igfitidea点击:

DataTables how do I show only date rather than datetime

jquerydatatables

提问by half_full

I'm using server side processing as per the Django examplein DataTables. I return datetime values in this format 'yyyy-mm-dd hh:mm:ss'. These date time values are currently displayed like this (for example):

我按照DataTables 中Django 示例使用服务器端处理。我以这种格式'yyyy-mm-dd hh:mm:ss' 返回日期时间值。这些日期时间值当前显示如下(例如):

Dec. 18, 2011, 11:59 p.m.

2011 年 12 月 18 日晚上 11:59

I would like to display just the date part rather than both date and time.

我想只显示日期部分而不是日期和时间。

This is what I have in my html page:

这是我的 html 页面中的内容:

<script type="text/javascript">
/* <![CDATA[ */
$(document).ready(function() {
        $('#certs-table').dataTable({
                "bJQueryUI": true,
                "sPaginationType": "full_numbers",
                "bProcessing": true,
                "bServerSide": true, 
                "sAjaxSource": "/cars/get_cars_list/",
                "iDisplayLength": 10,
                "aoColumnDefs": [
                        { "aTargets": [0], "bVisible": false, "bSearchable": false},
                        { 
                          "aTargets": [1], 
                           "fnRender": function ( oObj ) {
                                return '<a href=\"/cars/' + oObj.aData[0] + '/\">' + oObj.aData[1] + '</a>';
                            },
                          "bSearchable": true, 
                        },
                        { "aTargets": [2], "bSearchable": true},
                        { "aTargets": [3], "bSearchable": false, "sType": 'date'},
                ]
        });
} );
/* ]]> */
</script>

The date is is the 4th column, i.e., aTarget[3].

日期是第 4 列,即 aTarget[3]。

How do I display just the date portion please? I only started using DataTables/JQuery yesterday, so would really appreciate an example. Thanks.

请问如何只显示日期部分?我昨天才开始使用 DataTables/JQuery,所以非常感谢一个例子。谢谢。

采纳答案by Nicola Peluchetti

I think that the best whay would be to convert data server side and just return the date format you want, but you could also

我认为最好的办法是转换数据服务器端并只返回您想要的日期格式,但您也可以

 { "aTargets": [3], 
   "bSearchable": false, 
   "sType": 'date',
   "fnRender": function ( oObj ) {
              var javascriptDate = new Date(oObj.aData[0]);
              javascriptDate = javascriptDate.getDate()+"/"+javascriptDate.getMonth()+"/"+javascriptDate.getFullYear();
              return "<div class= date>"+javascriptDate+"<div>";
              }
 }

回答by Alex

It's often best to return the UTC date as JSON string from the server and then to format it in the browser with the user's local format settings. I like to use moment.jsto do the formatting. Example:

通常最好将 UTC 日期作为 JSON 字符串从服务器返回,然后在浏览器中使用用户的本地格式设置对其进行格式化。我喜欢使用moment.js进行格式化。例子:

"aoColumnDefs": [
    {   //format the date for local time
        "aTargets" : [ 1, 5], //indexes of whatever columns you need to format
        "mRender": function ( data, type, full ) {
            if(data){
                var mDate = moment(data);
                return (mDate && mDate.isValid()) ? mDate.format("L LT") : "";
            }
            return "";
        }
    }
]

There are different strings you can return that will work with moment. Make sure you specify the offset (Z) as +0000otherwise the date will be parsed as local time.

您可以返回不同的字符串,这些字符串适用于 moment。确保指定偏移量 (Z),+0000否则日期将被解析为本地时间。

moment("2010-10-20 4:30"); // parsed as 4:30 local time
moment("2010-10-20 4:30 +0000"); // parsed as 4:30 UTC 

回答by devlin carnate

The previous responses are using legacy DataTables API. The syntax for versions 1.10+is:

之前的响应使用的是旧版 DataTables API。1.10+ 版本语法是:

"columnDefs": [
    {
        "targets": [6, 7],
        "type" : "date",
        "render": function (data) {
            if (data !== null) {
                var javascriptDate = new Date(data);
                javascriptDate = javascriptDate.getMonth() + 1 + "/" + javascriptDate.getDate() + "/" + javascriptDate.getFullYear();
                return javascriptDate;
            } else {
                return "";
            }
        }
    }
]