判断半岁:
//两个日期
var date1 = '2013-03-26';
var date2 = '2011-01-10';
// 拆分年月日
date1 = date1.split('-');
// 得到月数
date1 = parseInt(date1[0]) * 12 + parseInt(date1[1]);
// 拆分年月日
date2 = date2.split('-');
// 得到月数
date2 = parseInt(date2[0]) * 12 + parseInt(date2[1]);
var m = Math.abs(date1 - date2);
alert(m);
封装方法:
formatAge: function(row) {
var dt = new Date(parseInt(row.Birthday.slice(6, 19)));
var year = dt.getFullYear();
var month = dt.getMonth() + 1;
var date = dt.getDate();
var nowDate = new Date();
var yearVal = nowDate.getFullYear()-year;
var monthVal =nowDate.getMonth()-month;
var age = 0;
if(monthVal >= 6){
age = yearVal + 0.5;
}
if (monthVal <6 && monthVal > -6) {
age = yearVal;
}
if(monthVal <= -6){
age = yearVal -0.5;
}
return age;
}
纯属个人观点,仅供参考!