数据表日期格式无法正常工作

时间:2013-09-25 10:10:19

标签: date datatable jquery-datatables

我的日期以数据格式显示在此格式中:“2013-09-02 12:00:00 AM”

我想要这种格式:“2013/09/02”

我用Google搜索并找到了一种方法。但是代码不起作用。输出显示“NaN / NaN / NaN”。

我的数据表脚本在这里:

$(document).ready(function () {
    $('#RequisitionTbl').dataTable({
        "aoColumnDefs": [
            {
                "aTargets": [0],
                "sType": 'date',
                "fnRender": function (oObj) {      // trying to format(like "2013/09/02") the date here

                    var javascriptDate = new Date(oObj.aData[0]);
                    javascriptDate = javascriptDate.getDate() + "/" + javascriptDate.getMonth() + "/" + javascriptDate.getFullYear();
                    return "<div class= date>" + javascriptDate + "<div>";
                }
            },

            { "sWidth": "350px", "aTargets": [0] },
            { "sWidth": "180px", "aTargets": [1] },
            { "sWidth": "200px", "aTargets": [2] },
            { "bSortable": false, "aTargets": [4] },
            { "bSearchable": false, "aTargets": [4] }
        ],
        "sDom": "<'row-fluid'<'span6'l><'span6'f>r>t<'row-fluid'<'span6'i><'span6'p>>",
        "sPaginationType": "bootstrap",
        "oLanguage": {
            "sLengthMenu": "Display _MENU_ records"
        }
    }).columnFilter({
        sPlaceHolder: "head:before",
        aoColumns: [
                        { type: 'date-range' }, ,{},]
    });
});

需要帮助来解决问题。

1 个答案:

答案 0 :(得分:1)

请记住,Month是从零开始的索引:

var currentDate = new Date();
var day = currentDate.getDate();
var month = currentDate.getMonth() + 1;
var year = currentDate.getFullYear();

var javascriptDate = year + '/' + month + '/' + day // Modify as you need

希望这有帮助!