健康一贴灵,专注医药行业管理信息化

json中的日期格式转换(扩展new date()显示格式)

在java  spring mvc 开发过程中,通过json 格式,向前端传递数据,日期格式发生了转变,

在前台数据展示时,要进行一定格式的转换才能正常显示;

我在开发中使用了easy ui 和my 97的日历控件,探索良久,得以解决。主要方法如下:

1、扩展new date()的显示格式,函数如下:

// 对Date的扩展,将 Date 转化为指定格式的String
    // 月(M)、日(d)、小时(h)、分(m)、秒(s)、季度(q) 可以用 1-2 个占位符, 
    // 年(y)可以用 1-4 个占位符,毫秒(S)只能用 1 个占位符(是 1-3 位的数字) 
    Date.prototype.Format = function (fmt) { //author: meizz 
        var o = {
            "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+)/.test(fmt)) fmt = fmt.replace(RegExp.$1, (this.getFullYear() + "").substr(4 - RegExp.$1.length));
        for (var k in o)
            if (new RegExp("(" + k + ")").test(fmt)) fmt = fmt.replace(RegExp.$1, (RegExp.$1.length == 1) ? (o[k]) : (("00" + o[k]).substr(("" + o[k]).length)));
        return fmt;
    }

 

(new Date()).Format("yyyy-MM-dd hh:mm:ss.S") //输出结果: 2018-02-03 08:36:10.400
(new Date()).Format("yyyy-M-d h:m:s.S")      //输出结果: 2018-2-03 9:36:35.572

实际应用例子如下:

  

 

posted @ 2018-04-08 10:07  一贴灵  阅读(2211)  评论(0编辑  收藏  举报
学以致用,效率第一