728x90
반응형
currentDate.Year와 birthDate.Year를 사용하여 현재 연도와 생년 연도를 가져온 다음, 두 가지 값을 빼서 연령을 계산한다. 그런 다음 생일이 지났는지 체크해서 연을 조정한다.
using System;
class Program
{
static void Main(string[] args)
{
// 현재 날짜
DateTime currentDate = DateTime.Now;
// 생년월일 입력
Console.WriteLine("생년월일을 입력하세요 (예: 1990-01-01): ");
DateTime birthDate = DateTime.Parse(Console.ReadLine());
// 만 나이 계산
int age = currentDate.Year - birthDate.Year;
// 생일이 지났는지 체크
if (currentDate.Month < birthDate.Month || (currentDate.Month == birthDate.Month && currentDate.Day < birthDate.Day))
{
age--;
}
// 결과 출력
Console.WriteLine("만 나이: " + age);
}
}
출처 | chatGPT
728x90
반응형
'.NET' 카테고리의 다른 글
C# 에서 의존성 주입 사용하는가 (Dependency Injection) | ChatGPT (0) | 2023.08.25 |
---|---|
cshtml 파일에서 function 과 helper 차이 및 사용 용도 | ChatGPT (0) | 2023.08.24 |
System.Data.SqlClient.SqlParameter 를 사용하여 nvarchar(max) 코드 설정하는 방법 (chatGPT) (0) | 2023.07.30 |
HttpContext.Current.Response.Write 에 개행 \n 문자 넣는 방법 (chatGPT) (0) | 2023.07.29 |
static 을 왜 사용하는가, 사용하는 이유 (ChatGPT) (0) | 2023.07.27 |