DJ메탈짱™의 Free Style

[C#] 확장메소드, Extension, Method, 생년월일, 만 나이 계산, ParseExact, DateTime 본문

일(job)/MS(Microsoft)

[C#] 확장메소드, Extension, Method, 생년월일, 만 나이 계산, ParseExact, DateTime

뽀&쏭 2015. 12. 21. 12:35



/// <summary>

/// 현재시간을 기준으로 생년월일을 가지고 만 나이 계산

/// </summary>

/// <param name="strDob">생년월일</param>

/// <param name="dtKST">현재시간</param>

/// <returns>나이</returns>

public static int GetAge(this string strDob, DateTime dtKST)

{

// 현재 서버시간(KST)

        DateTime dtNow = Convert.ToDateTime(dtKST);

 

        // 선택한 날짜 Convert수정

        DateTime dtDOB = DateTime.ParseExact(strDob, "yyyyMMdd"null);

           

        int iAge = 0;

        int birth_year = dtDOB.Year;

        int birth_month = dtDOB.Month;

        int birth_day = dtDOB.Day;

 

        int now_year = dtNow.Year; ;

        int now_month = dtNow.Month;

        int now_day = dtNow.Day;

 

if (birth_month < now_month)

                       iAge = now_year - birth_year;

else if (birth_month == now_month)

        {

                       if (birth_day <= now_day)

                 iAge = now_year - birth_year;

                   else

                 iAge = now_year - birth_year - 1;

}

        else

                       iAge = now_year - birth_year - 1;

 

 

return iAge;

}

 

 

 

// 생년월일 기준으로 만 나이 계산하기.

DateTime dtKST = DateTime.ParseExact(_dtServerTime, "yyyyMMdd"null); // 서버시간 반환

int iAge = this.txtDOB.Text.GetAge(dtKST);

 

if (iAge < 11)

{

sChild = "CHD";

}