var DateFormater = {
/**
* 格式化日期(不含时间)
*/
formatterDate: function (date) {
if (date == undefined) {
return "";
}
date = new Date(date);
if (date.getFullYear() < 1900) {
return "";
}
var datetime = date.getFullYear()
+ "-"// "年"
+ (date.getMonth() >= 9 ? (date.getMonth() + 1) : "0" + (date.getMonth() + 1))
+ "-"// "月"
+ (date.getDate() < 10 ? "0" + date.getDate() : date.getDate());
return datetime;
},
/**
* 格式化去日期(含时间)
*/
formatterDateTime: function (date) {
if (date == undefined) {
return "";
}
date = new Date(date);
if (date.getFullYear() < 1900) {
return "";
}
var datetime = date.getFullYear()
+ "-"// "年"
+ (date.getMonth() >= 9 ? (date.getMonth() + 1) : "0" + (date.getMonth() + 1))
+ "-"// "月"
+ (date.getDate() < 10 ? "0" + date.getDate() : date.getDate())
+ " "
+ (date.getHours() < 10 ? "0" + date.getHours() : date.getHours())
+ ":"
+ (date.getMinutes() < 10 ? "0" + date.getMinutes() : date.getMinutes())
+ ":"
+ (date.getSeconds() < 10 ? "0" + date.getSeconds() : date.getSeconds());
return datetime;
},
/**
* 格式化去日期(含时分)
*/
formatterDateHourMinute: function (date) {
if (date == undefined) {
return "";
}
date = new Date(date);
if (date.getFullYear() < 1900) {
return "";
}
var datetime = date.getFullYear()
+ "-"// "年"
+ (date.getMonth() >= 9 ? (date.getMonth() + 1) : "0" + (date.getMonth() + 1))
+ "-"// "月"
+ (date.getDate() < 10 ? "0" + date.getDate() : date.getDate())
+ " "
+ (date.getHours() < 10 ? "0" + date.getHours() : date.getHours())
+ ":"
+ (date.getMinutes() < 10 ? "0" + date.getMinutes() : date.getMinutes());
return datetime;
},
formatYearAndMonth: function(dateboxId) {
$('#' + dateboxId).datebox({
formatter: function(date) {
var y = date.getFullYear();
var m = date.getMonth() + 1;
return y + '-' + (m < 10 ? ('0' + m) : m);
},
parser: function(s) {
if (!s) {
return new Date();
}
var ss = s.split('-');
var y = parseInt(ss[0], 10);
var m = parseInt(ss[1], 10);
if (!isNaN(y) && !isNaN(m)) {
return new Date(y, m - 1);
} else {
return new Date();
}
}
});
},
// 获取startDate和endDate相隔天数,格式:yyyy-MM-dd
getDaysNum: function(startDate, endDate) {
if (startDate == undefined || endDate == undefined) {
return 0;
}
try {
var startTime = new Date(Date.parse(startDate.replace(/-/g, "/"))).getTime();
var endTime = new Date(Date.parse(endDate.replace(/-/g, "/"))).getTime();
var dates = (endTime - startTime)/(1000*60*60*24);
return dates;
} catch (err) {
return 0;
}
}
};
//转换方法
var escapeChar = function(haveSpecialval) {
if(haveSpecialval != null && haveSpecialval.length > 0){
haveSpecialval = haveSpecialval.replace(/\'/g, "\\\'");
// 转换半角双引号
haveSpecialval = haveSpecialval.replace(/\"/g, """);
haveSpecialval = haveSpecialval.replace(/"/g, "\"");
haveSpecialval = haveSpecialval.replace(/'/g, "\'");
}
return haveSpecialval;
};
// 自定义serialize转serializeJson
(function($){
$.fn.serializeJson = function(){
var serializeObj={};
$(this.serializeArray()).each(function(){
serializeObj[this.name]=this.value;
});
return serializeObj;
};
})(jQuery);