設定身份證號與性別、生日聯動,根據當前時間戳獲取年齡
1、設定身份證號與性別、生日聯動
$("#sfz").change(function(){
//設定身份證號生日聯動
$("#birthDate").val($("#sfz").val().substring(6,10)+"-"+$("#sfz").val().substring(10,12)+"-"+$("#sfz").val().substring(12,14));
//設定省份證號性別聯動
$("#sex").val((($("#sfz").val().substring(16,17))%2==0)?'女':'男');
});
2、根據當前時間戳獲取年齡
{
field: 'birthDate',
title: '年齡',
sortable: true,
formatter:function(value, row , index){
//根據時間戳格式化當前時間
var date = new Date(value);
Y = date.getFullYear() + '-';
M = (date.getMonth()+1 < 10 ? '0'+(date.getMonth()+1) : date.getMonth()+1) + '-';
D = (date.getDate()+1 < 10 ? '0'+(date.getDate()+1) : date.getDate()+1); + ' ';
str = Y+M+D;
//根據當前時間計算年齡
var r = str.match(/^(\d{1,4})(-|\/)(\d{1,2})\2(\d{1,2})$/);
if(r==null)return false;
var d= new Date(r[1], r[3]-1, r[4]);
if (d.getFullYear()==r[1]&&(d.getMonth()+1)==r[3]&&d.getDate()==r[4])
{
var Y = new Date().getFullYear();
return((Y-r[1]));
}
return("-");
}
}