js获计算时间差取年份
//传递开始、结束时间计算设置值 function SetCompanyAge() { var startDateStr = DLMISForm_GetControl("HR_BasicInfo", "StartWork", -1).value;//入职时间 var endDateStr = getNowFormatDate();//当前时间 var year = getDateYearSub(startDateStr, endDateStr); DLMISForm_SetControl("HR_BasicInfo", "CompanyAge", -1, year); } //计算年份 function getDateYearSub(startDateStr, endDateStr) { var day = 24 * 60 * 60 *1000; var sDate = new Date(Date.parse(startDateStr.replace(/-/g, "/"))); var eDate = new Date(Date.parse(endDateStr.replace(/-/g, "/"))); //得到前一天(算头不算尾) sDate = new Date(sDate.getTime() - day); //获得各自的年、月、日 var sY = sDate.getFullYear(); var sM = sDate.getMonth()+1; var sD = sDate.getDate(); var eY = eDate.getFullYear(); var eM = eDate.getMonth()+1; var eD = eDate.getDate(); if(eY > sY && sM == eM && sD == eD) { return eY - sY; } else { //alert("两个日期之间并非整年,请重新选择"); return 0; } } //获取当前时间 function getNowFormatDate() { var date = new Date(); var seperator1 = "-"; var year = date.getFullYear(); var month = date.getMonth() + 1; var strDate = date.getDate(); if (month >= 1 && month <= 9) { month = "0" + month; } if (strDate >= 0 && strDate <= 9) { strDate = "0" + strDate; } var currentdate = year + seperator1 + month + seperator1 + strDate; return currentdate; }