프로젝트에서 사용한 소스와 http://koxo.com/ 사이트의 자료를 기반으로하여 좀 더 정확한 계산이
되도록 내 나름대로 개선하여 정리한다.
//param : pStartDate - 시작일 //param : pEndDate - 마지막일 //param : pType - 'D':일수, 'M':개월수 // Update. 2014.11.07. 변수명 변경 : strGapDT->strTermCnt // Update. 2014.11.07. 개월수 계산 시 년도가 다른 경우 부정확성 보완 : floor->round AND 365.25->365 function fn_calcDayMonthCount(pStartDate, pEndDate, pType) { var strSDT = new Date(pStartDate.substring(0,4),pStartDate.substring(4,6)-1,pStartDate.substring(6,8)); var strEDT = new Date(pEndDate.substring(0,4),pEndDate.substring(4,6)-1,pEndDate.substring(6,8)); var strTermCnt = 0; if(pType == 'D') { //일수 차이 strTermCnt = (strEDT.getTime()-strSDT.getTime())/(1000*60*60*24); } else { //개월수 차이 //년도가 같으면 단순히 월을 마이너스 한다. // => 20090301-20090201 의 경우(윤달이 있는 경우) 아래 else의 로직으로는 정상적인 1이 리턴되지 않는다. if(pEndDate.substring(0,4) == pStartDate.substring(0,4)) { strTermCnt = pEndDate.substring(4,6) * 1 - pStartDate.substring(4,6) * 1; } else { //strTermCnt = Math.floor((strEDT.getTime()-strSDT.getTime())/(1000*60*60*24*365.25/12)); strTermCnt = Math.round((strEDT.getTime()-strSDT.getTime())/(1000*60*60*24*365/12)); } } return strTermCnt; }
감사합니다.
답글삭제덕분에 시간을 단축해서 처리 했네요. ^^
근데, 제가 수정을 좀 해서 처리를 했는데요. 년도가 바뀌면 201311-201411 차이수가 12가 이니라 11로 나오네요. ^^
다시해보니깐...
답글삭제년월관련해서 값이 정확히 반환하지 못하네요.
년도가 바뀌면 값이 이상하게 꼬입니다.
흠... 그런 문제가 있어요?
삭제예전에 구현할 때 테스트를 다 했었는데... 다시 확인해 봐야겠네요.
피드백 감사합니다.