728x90
간만에 C# 코드 하나 올립니다. 굉장히 오랜 시간동안 개발 관련 포스팅을 하지 않았는데
앞으로 기회가 또 있을지 모르겠지만 좋은 코드가 있으면 공유하겠습니다.
그리고 좋은방법 있으면 의견 주세요.....
using System;
using System.Collections.Generic;
using System.Text;
namespace DurationDay
{
///
/// 어떤 시작과 종료 되는 시점의 기간(duration)을 일정 지정한 간격만큼
/// 배분하는 로직
///
class Program
{
static void Main(string[] args)
{
// 시작/종료일
string fromDate = "2009-05-04";
string toDate = "2012-05-03";
// 시작/종료일 날짜형 변환
DateTime fromDt = DateTime.Parse(fromDate);
DateTime toDt = DateTime.Parse(toDate);
// 날짜 기간 (분모)
int duration = DayCount(fromDt, toDt);
// 기간 등분 (분자)
int numerator = 19;
// 날짜 사이의 간격(몫)
int portion = duration / numerator;
// 나머지
int mod = duration % numerator;
Console.WriteLine("duration : {0}", duration);
Console.WriteLine("numerator : {0}", numerator);
Console.WriteLine("portion : {0}", portion);
Console.WriteLine("mod : {0}", mod);
int add = 0;
int i = 0;
int addDay = 0;
DateTime day = new DateTime();
// 전체 날짜 기간이 20일 이상일 경우만 간격지정
if (duration > numerator)
{
// 기간 등분만큼 Loop 반복
do
{
if (i == 0) // 시작일
{
day = fromDt;
add = 0;
addDay = 0;
}
else if (i == numerator) // 종료일
{
day = toDt;
add += portion;
addDay = portion;
}
else if (i >= 1 && i <= 19)
{
// 나머지 수 만큼 앞에서부터 +1을 더함
if (i <= mod)
{
add = i * (portion + 1);
addDay = (portion + 1);
day = fromDt.AddDays(add - 1);
}
else
{
add += (portion);
addDay = portion;
day = fromDt.AddDays(add);
}
}
i++;
Console.WriteLine("{0} : {1} / {2} / {3}",
i >= 10 ? i.ToString() : "0" + i.ToString(),
day.ToString("yyyy-MM-dd"), add, addDay);
} while (i <= numerator);
}
Console.ReadLine();
}
static int DayCount(DateTime fromDt, DateTime ToDt)
{
TimeSpan ts = ToDt - fromDt;
return ts.Days;
}
}
}
소스 코드는 Visual Studio 2008 (.net 3.5) 환경에서 개발되었습니다.
받으신 다음 테스트 해보시면 금방 이해되실 수 있습니다.
numerator, 시작일, 종료일 값만 변경하여 테스트 하시면 됩니다.
'.NET C#' 카테고리의 다른 글
| C# GPX(GPS Exchange Format) Paser (0) | 2023.01.13 |
|---|---|
| 코드 블럭 테스트 (c# DB Helper) (0) | 2023.01.12 |
| 시작일과 종료일 기간 체크 및 날짜 등분 (0) | 2012.05.05 |
| Visual Studio 2010 and .NET Framework 4.0 Training Kit - November Preview (0) | 2008.11.30 |
| SQL 2008 설치전 몇가지 고려사항 (0) | 2008.11.25 |
| Microsoft .Net의 새로운 로고입니다. (8) | 2008.10.29 |
| 2008 PDC에서 Silverlight Tools 및 Toolkit release & Themes 가 발표되었습니다. (2) | 2008.10.29 |
DurationDay.zip
댓글