easyUI datagrid 時間格式化


從后台傳過來的數據,其中含有日期字段,那么在前端的easyUI這里顯示的話,會顯得比較怪異,一大串,中間是個T,后面一大堆零,不知道是什么意思。

看來要進行格式化。

問題是,在哪里格式化?

如果在后端,轉換成想要的字符串,易如反掌。但細想,邏輯上比較合適在前端處理:時間數據從后台過來,然后如何顯示,是前端的事。

那么在前端這里,如何處理?

1、擴展Date的功能

Date.prototype.format = function (format) {
    var o = {
        "M+": this.getMonth() + 1, // month
        "d+": this.getDate(), // day
        "h+": this.getHours(), // hour
        "m+": this.getMinutes(), // minute
        "s+": this.getSeconds(), // second
        "q+": Math.floor((this.getMonth() + 3) / 3), // quarter
        "S": this.getMilliseconds()
        // millisecond
    }
    if (/(y+)/.test(format))
        format = format.replace(RegExp.$1, (this.getFullYear() + "")
            .substr(4 - RegExp.$1.length));
    for (var k in o)
        if (new RegExp("(" + k + ")").test(format))
            format = format.replace(RegExp.$1, RegExp.$1.length == 1 ? o[k] : ("00" + o[k]).substr(("" + o[k]).length));
    return format;
}
function formatDatebox(value) {
    if (value == null || value == '') {
        return '';
    }
    var dt;
    if (value instanceof Date) {
        dt = value;
    } else {
        dt = new Date(value);
    }

    return dt.format("yyyy-MM-dd"); //擴展的Date的format方法(上述插件實現)
}

2、easyUI datagrid 中調用

$('#datagridDemo1').datagrid({
    ……
    columns: [[
            { field: 'CreateDate', title: '創建日期', width: 120,align: 'center',formatter: formatDatebox},
            ……
        ]]    
});

如此,可顯示格式化日期矣。


參考文章:

http://blog.csdn.net/walkerjong/article/details/7514026

版權聲明:本文為博主原屙文章,喜歡你就擔走。


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM