vb.net DataTables 插件:如何在 DataTable 插件中格式化日期列?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/28500449/
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-09-17 18:48:13  来源:igfitidea点击:

DataTables Plugin: How to format a date column in DataTable plugin?

javascriptjqueryvb.netdatatables

提问by David

I am new in the DataTable Plugin for jquery.

我是 jquery 的 DataTable 插件的新手。

I would like to convert a column date to dd/mm/yyyybut it is returning me in the table this format: Wed Jan 09 2013 00:00:00 GMT+0100 (Hora estándar romance).

我想将一列日期转换为,dd/mm/yyyy但它在表中以这种格式返回我:Wed Jan 09 2013 00:00:00 GMT+0100 (Hora estándar romance).

$('#tblProceso').dataTable({
    "data": dataSet.dataList,
    "columns": [
                { "title": "my date", "data": "mydate" }
    ]
});

Class I am using for it is

我使用的类是

Public class DateClass {
  Public Property mydate As DateTime
}

this process is called in ajax function and in the response I assign the list of DateClass.

这个过程在 ajax 函数中调用,在响应中我分配了 DateClass 列表。

How can I format the column? What do I need to add?

如何格式化列?我需要添加什么?

回答by David Sopko

If you are returning a nullable date time the render function needs to work a little different:

如果您要返回可为空的日期时间,则渲染函数的工作方式需要稍有不同:

        $('#tblProceso').DataTable({
        columns: [

            {"title": "my date",
             "data": "mydate",
             "type": "date ",
             "render":function (value) {
                 if (value === null) return "";

                  var pattern = /Date\(([^)]+)\)/;
                  var results = pattern.exec(value);
                  var dt = new Date(parseFloat(results[1]));

                  return (dt.getMonth() + 1) + "/" + dt.getDate() + "/" + dt.getFullYear();}
            }
        ]};

回答by Samuel Diogo

my approach using Moment.jsbased at David Sopkpo answer:

用我的方法Moment.js设在大卫Sopkpo答案

        $('#tblProceso').DataTable({
        columns: [

            {"title": "my date",
             "data": "mydate",
             "type": "date ",
             "render":function (value) {
                  if (value === null) return "";
                  return moment(value).format('DD/MM/YYYY');
                 }
            }
        ]};

回答by Nilesh Pethani

On method would be like this,

方法是这样的,

When you create date list at server side, store date in required format (dd/mm/yy). So when you make Ajax call and get list of date, it will came from server by default format you want.

在服务器端创建日期列表时,以所需格式 (dd/mm/yy) 存储日期。因此,当您进行 Ajax 调用并获取日期列表时,它将以您想要的默认格式来自服务器。

*sorry for my bad English.

*对不起,我的英语不好。

回答by Castro Roy

if you want to format it with javascript, you need to override of the fnRenderfunction of the column definition. Something like this:

如果你想用javascript格式化它,你需要覆盖fnRender列定义的功能。像这样的东西:

$("#tblProceso").dataTable({
  "data": dataSet.dataList,
  "aoColumnDefs": [
    { 
        "aTargets": [0], //column index counting from the left
        "sType": 'date',
        "fnRender": function ( dateObj ) {
            var oDate = new Date(dateObj.aData[0]);
            result = oDate.getDate()+"/"+(oDate.getMonth()+1)+"/"+oDate.getFullYear();
            return "<span>"+result+"</span>";
        }
    }
  ],
  "columns": [
            { "title": "my date", "data": "mydate" }
  ]
});

with datatablesif you have a column with dates, you will have issues when sorting, so, use this plugin

datatables如果您有日期的列,你将有问题排序时,所以,使用这个插件