NutzCN Logo
问答 js处理日期格式YYYY-MM-DD
发布于 2994天前 作者 Hamming 2188 次浏览 复制 上一个帖子 下一个帖子
标签:

目前显示日期是
2000-01-01 00:00:00
看nutzwk里面有日期处理是这个 但是我的日期是datetime改如处理呢

                {
                    "render": function (data, type, row) {
                        if (data) {
                            return moment(parseInt(data*1000)).format("YYYY-MM-DD HH:mm:ss");
                        }
                        return "";
                    },
                    "targets": 4
                },
7 回复

<script language="JavaScript"> var datatable; function initDatatable() { datatable = $('.datatable').DataTable({ "dom": '<"toolbar">frtip', "searching":false, "processing": false, "serverSide": true, "select": true, "ordering": true, "language": { "url": "${base}/assets/plugins/datatables/cn.json" }, "preDrawCallback": function () { sublime.showLoadingbar($(".main-content")); }, "drawCallback": function () { sublime.closeLoadingbar($(".main-content")); }, "ajax": { "url": "${base}/private/sys/task/data", "type": "post", "data": function (d) { } }, // "order": [[0, "desc"]], "columns": [ {"data": "name", "bSortable": true}, {"data": "jobClass", "bSortable": true}, {"data": "cron", "bSortable": true}, {"data": "disabled", "bSortable": true}, {"data": "exeAt", "bSortable": true}, {"data": "exeResult", "bSortable": true} ], "columnDefs": [ { "render": function (data, type, row) { if (!data) { return '<i id="disable_' + row.id + '" class="fa fa-circle text-success ml5"></i>'; } else { return '<i id="disable_' + row.id + '" class="fa fa-circle text-danger ml5"></i>'; } }, "targets": 3 }, { "render": function (data, type, row) { if (data) { return moment(parseInt(data*1000)).format("YYYY-MM-DD HH:mm:ss"); } return ""; }, "targets": 4 }, { "render": function (data, type, row) { return '<div class="btn-group"><button type="button" class="btn btn-default btn-xs dropdown-toggle" data-toggle="dropdown">' + ' <i class="ti-settings"></i> <span class="ti-angle-down"></span></button><ul class="dropdown-menu" role="menu">' + '<li><a href="${base}/private/sys/task/edit/' + row.id + '" data-pjax>修改</a></li>' + '<li class="divider"></li>' + '<li><a href="javascript:;" onclick="del(\'' + row.id + '\')">删除</a></li>' + '<li class="divider"></li>' + '<li><a href="javascript:;" onclick="enableTask(\'' + row.id + '\')">启用</a></li>' + '<li><a href="javascript:;" onclick="disableTask(\'' + row.id + '\')">禁用</a></li>' + '</ul></div>'; }, "targets": 6 } ] }); datatable.on('click', 'tr', function () { $(this).toggleClass('selected'); }); $("#searchBtn").on('click', function () { datatable.ajax.reload(); }); } function enableTask(id) { $.post("${base}/private/sys/task/enable/" + id, {}, function (data) { if (data.code == 0) { $("#disable_" + id).attr("class", "fa fa-circle text-success ml5"); } else { Toast.error(data.msg); } }, "json"); } function disableTask(id) { $.post("${base}/private/sys/task/disable/" + id, {}, function (data) { if (data.code == 0) { $("#disable_" + id).attr("class", "fa fa-circle text-danger ml5"); } else { Toast.error(data.msg); } }, "json"); } function del(id) { var dialog = $("#dialogDelete"); dialog.modal("show"); dialog.find("#okDel").unbind("click"); dialog.find("#okDel").bind("click", function (event) { var btn = $(this); btn.button("loading"); $.post("${base}/private/sys/task/delete/" + id, {}, function (data) { if (data.code == 0) { datatable.ajax.reload(null,false); } else { Toast.error(data.msg); } //重置按钮状态,关闭提示框 btn.button("reset"); dialog.modal("hide"); }, "json"); }); } function delCheck() { var chks = datatable.rows('.selected').data(); if (chks.length > 0) { var ids = []; $.each(datatable.rows('.selected').data(), function (i, n) { ids.push(n.id); }); var dialog = $("#dialogDelete"); dialog.modal("show"); dialog.find("#okDel").unbind("click"); dialog.find("#okDel").bind("click", function (event) { var btn = $(this); btn.button("loading"); $.post("${base}/private/sys/task/delete", {ids: ids.toString()}, function (data) { if (data.code == 0) { datatable.ajax.reload(null,false); } else { Toast.error(data.msg); } btn.button("reset"); dialog.modal("hide"); }, "json"); }); } else { Toast.warning("请先选择要删除的项!"); } } $(function () { initDatatable(); }); </script>

返回日期改成如下 还是出错

                {
                    "render": function (data, type, row) {
                        if (data) {
                            return formatDate((data),"yyyy-MM-dd");
                        }
                        return "";
                    },
                    "targets": 4
                },

@tonyyule js的date对象只有一种吧?

@wendal
最终改成酱紫 可以了

                    "render": function (data, type, row) {
                        if (data) {
                            var date = new Date(data);
                            var str = date.getFullYear() + "-" + (date.getMonth() + 1) + "-" + date.getDate();
                            return str;
                        }
                        return "";
                    },

数据库里存的是时间戳,如果是字符串的话,自行搜索moment插件api用法

Date.prototype.format = function (format) {
    var date = {
        "M+": this.getMonth() + 1,
        "d+": this.getDate(),
        "h+": this.getHours(),
        "m+": this.getMinutes(),
        "s+": this.getSeconds(),
        "q+": Math.floor((this.getMonth() + 3) / 3),
        "S+": this.getMilliseconds()
    };
    if (/(y+)/i.test(format)) {
        format = format.replace(RegExp.$1, (this.getFullYear() + '').substr(4 - RegExp.$1.length));
    }
    for (var k in date) {
        if (new RegExp("(" + k + ")").test(format)) {
            format = format.replace(RegExp.$1, RegExp.$1.length == 1 ? date[k] : ("00" + date[k]).substr(("" + date[k]).length));
        }
    }
    return format;
}

@tonyyule 但是,js的月份有问题,是从0开始的

来自炫酷的 NutzCN

添加回复
请先登陆
回到顶部