개발 업무/Java Script

주민등록번호 생년월일 추출

호크아이나인 2023. 2. 9. 10:39

/**
 * 주민등록번호로부터 생년월일 추출
 * 
 * @author kyoungsoo lee
 * @date 2023. 01. 26.
 * @memberOf strLib
 * @param {string} source - 원본 문자열
 * @return {string} 결과 
 */
strLib.getBirthdayFromRRN = function(source) {
    try {
        source = source.replace(/\D/ig, "");
        if (source.length < 7 || isNaN(source)) {
            // 개발자 메시지: 영문지원 안 함
            throw "주민등록번호가 너무 짧거나 올바르지 않습니다.";
        }

        var yearPrefix = "";
        var yearIndexer = source.substr(6, 1);
        if (yearIndexer == "9" || yearIndexer == "0") {
            yearPrefix = "18";
        } else if (yearIndexer == "1" || yearIndexer == "2") {
            yearPrefix = "19";
        } else if (yearIndexer == "3" || yearIndexer == "4") {
            yearPrefix = "20";
        } else {
            return "";
        }

        return yearPrefix + source.substr(0, 2) + source.substr(2, 2) + source.substr(4, 2);
    } catch (e) {
        console.log("strLib.getBirthdayFromRRN: " + e);
    }
};