从后台返回的日期类型的数据,若是直接在前端进行显示的话,显示的就是一个从 1970-01-01 00:00:00到如今所通过的毫秒数,而在大多数业务中都不可能显示这个毫秒数,大多数都是显示一个正常的日期格式,因此在这里,咱们使用js对于从后台返回的Date类型的数据进行处理.前端
方法1、函数
common.js代码this
//日期格式化,将毫秒转为 XXXX-XX-XX 的格式
Date.prototype.Format = function(fmt) {
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;spa
};prototype
其余js中进行使用:code
方法2、orm
common.js代码:对象
var Common = function () { return { // 初始化各个函数及对象 init: function () { }, strIsNotEmpty: function(str) { if (str != null && str != undefined && str != '') { return true; } return false; }, // 时间戳转换成指定日期格式 formatTime: function(time, format) { var t = new Date(time); var tf = function(i){return (i < 10 ? '0' : '') + i}; return format.replace(/yyyy|MM|dd|HH|mm|ss/g, function(a){ switch(a){ case 'yyyy': return tf(t.getFullYear()); break; case 'MM': return tf(t.getMonth() + 1); break; case 'mm': return tf(t.getMinutes()); break; case 'dd': return tf(t.getDate()); break; case 'HH': return tf(t.getHours()); break; case 'ss': return tf(t.getSeconds()); break; } }) } }; }(); jQuery(document).ready(function() { Common.init(); });
在其余js中使用:blog