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; }
相關文章
相關標籤/搜索