1. 字符转化为日期后相减
例子: var b_time;
var f_time;
//.replace(/-/g,"/"))转换格式方法若漏写会转化错误
b_time=new Date($("#begin_time").val().replace(/-/g,"/"));
f_time=new Date($("#final_time").val().replace(/-/g,"/"));
//时间相减返回值为秒,此结果计算所得值返回为月份
var month = Math.round((f_time - b_time)/1000/60/60/24/30);
2. JSON时间格式转换
参数如 /Date(1354648740000)/ 这样的格式,返回为2012-12-05 03:17:0.810
/*时间格式转换*/
function dateFormat(dt, type) {
if (dt == null)
return "";
var time = parseInt(dt.replace(/\D/igm, ""));
var datetime = new Date();
datetime.setTime(time);
var year = datetime.getFullYear();
var month = datetime.getMonth() + 1 < 10 ? "0" + (datetime.getMonth() + 1) : datetime.getMonth() + 1;
var date = datetime.getDate() < 10 ? "0" + datetime.getDate() : datetime.getDate();
var hour = datetime.getHours() < 10 ? "0" + datetime.getHours() : datetime.getHours();
var minute = datetime.getMinutes() < 10 ? "0" + datetime.getMinutes() : datetime.getMinutes();
var second = datetime.getSeconds() < 10 ? "0" + datetime.getSeconds() : datetime.getSeconds();
var resultTime = year + "-" + month + "-" + date + " " + hour + ":" + minute + ":" + second;
if (resultTime == "1999-01-01 00:00:00")
return "";
else if (type == "ymd")
return year + "-" + month + "-" + date;
else
return resultTime;
}