function DateSer(y, m, d: Integer): TDateTime;
const
mj: array[1..12] of Integer=(31,28,31,30,31,30,31,31,30,31,30,31);
var
add: Integer;
begin
While (true) do
begin
y := y + (m-1) div 12;
m := (m-1) mod 12 +1;
if m <= 0 then
begin
Inc(m, 12);
Dec(y);
end;
if ((y mod 4 = 0) and
((y mod 100 <> 0) or (y mod 400 = 0))) and
(m = 2)
then
add:=1 //add one day in February
else
add:=0;
if (d > 0) and (d <= (mj[m] + add)) then
break;
if d > 0 then
begin
Dec(d, mj[m]+add);
Inc(m);
end
else
begin
Inc(d, mj[m]+add);
Dec(m);
end;
end;
Result := EncodeDate(y, m, d);
end;
// 사용예제
DecodeDate(Date, y, m, d);
NewDate := DateSer(y-4, m+254, d+1234);
type
TDayOfWeek = (Sunday,Monday,Tuesday,Wednesday,Thursday,Friday,Saturday);
type
TForm1 = class(TForm)
private
{ Private declarations }
public
{ Public declarations }
function dateYear(D: TDateTime): Integer;
function dateMonth(D: TDateTime): Integer;
function dateDay(D: TDateTime): Integer;
function dateBeginOfYear(D: TDateTime): TDateTime;
function dateEndOfYear(D: TDateTime): TDateTime;
function dateBeginOfMonth(D: TDateTime): TDateTime;
function dateEndOfMonth(D: TDateTime): TDateTime;
function dateWeekOfYear(D: TDateTime): Integer;
function dateDayOfYear(D: TDateTime): Integer;
function dateDayOfWeek(D: TDateTime): TDayOfWeek;
function dateLeapYear(D: TDateTime): Boolean;
function dateBeginOfQuarter(D: TDateTime): TDateTime;
function dateEndOfQuarter(D: TDateTime): TDateTime;
function dateBeginOfWeek(D: TDateTime;Weekday: Integer): TDateTime;
function dateDaysInMonth(D: TDateTime): Integer;
end;
var
Form1: TForm1;
implementation
{$R *.DFM}
// 날짜의 년도만 발취
function TForm1.dateYear(D: TDateTime): Integer;
var
Year, Month, Day: Word;
begin
DecodeDate(D, Year, Month, Day);
Result := Year;
end;
// 날짜의 월만 발취
function TForm1.dateMonth(D: TDateTime): Integer;
var
Year, Month, Day: Word;
begin
DecodeDate(D, Year, Month, Day);
Result := Month;
end;
// 날짜의 일만 발취
function TForm1.dateDay(D: TDateTime): Integer;
var
Year,Month,Day : Word;
begin
DecodeDate (D,Year,Month,Day);
Result := Day;
end;
// 년의 시작일자
function TForm1.dateBeginOfYear(D: TDateTime): TDateTime;
var
Year, Month, Day: Word;
begin
DecodeDate(D, Year, Month, Day);
Result := EncodeDate(Year, 1, 1);
end;
// 년의 마지막일자
function TForm1.dateEndOfYear(D: TDateTime): TDateTime;
var
Year, Month, Day: Word;
begin
DecodeDate(D, Year, Month, Day);
Result := EncodeDate(Year, 12, 31);
end;
// 월의 시작일자
function TForm1.dateBeginOfMonth(D: TDateTime): TDateTime;
var
Year, Month, Day: Word;
begin
DecodeDate(D, Year, Month, Day);
Result := EncodeDate(Year, Month, 1);
end;
// 월의 마지막일자
function TForm1.dateEndOfMonth(D: TDateTime): TDateTime;
var
Year, Month, Day: Word;
begin
DecodeDate(D, Year, Month, Day);
if Month = 12 then
begin
Inc(Year);
Month := 1;
end
else
Inc(Month);
{월의 다음월의 시작일에서 1을 뺀다}
Result := EncodeDate(Year, Month, 1) - 1;
end;
// 년을 기준으로 몇번째 주인지
function TForm1.dateWeekOfYear(D: TDateTime): Integer;
const
t1: array[1..7] of ShortInt = ( -1, 0, 1, 2, 3, -3, -2);
t2: array[1..7] of ShortInt = ( -4, 2, 1, 0, -1, -2, -3);
var
doy1, doy2: Integer;
NewYear: TDateTime;
begin
NewYear := dateBeginOfYear(D);
doy1 := dateDayofYear(D) + t1[DayOfWeek(NewYear)];
doy2 := dateDayofYear(D) + t2[DayOfWeek(D)];
if doy1 <= 0 then
Result := dateWeekOfYear(NewYear-1)
else if (doy2 >= dateDayofYear(dateEndOfYear(NewYear))) then
Result:= 1
else
Result:=(doy1-1) div 7+1;
end;
// 년을 기준으로 몇번째 일인지
function TForm1.dateDayOfYear(D: TDateTime): Integer;
begin
Result := Trunc(D-dateBeginOfYear(D)) + 1;
end;
// 요일 번호
function TForm1.dateDayOfWeek(D: TDateTime): TDayOfWeek;
begin
Result := TDayOfWeek(Pred(DayOfWeek(D)));
end;
// 윤년인지 검사
function TForm1.dateLeapYear(D: TDateTime): Boolean;
var
Year, Month, Day: Word;
begin
DecodeDate(D, Year, Month, Day);
// 100으로 나누어 떨어지고 400으로 나누어 떨어지지 않는 연도를 제외한
// 매 4년마다 한 번씩 더 많게 되거나 윤년이 된다
Result := (Year mod 4 = 0) and ((Year mod 100 <> 0) or (Year mod 400 = 0));
end;
// 주어진 일자가 포함된 분기의 시작일
function TForm1.dateBeginOfQuarter(D: TDateTime): TDateTime;
var
Year, Month, Day: Word;
begin
DecodeDate(D, Year, Month, Day);
Result := EncodeDate(Year, (((Month-1) div 3) * 3)+1, 1);
end;
// 주어진 일자가 포함된 분기의 마지막일
function TForm1.dateEndOfQuarter(D: TDateTime): TDateTime;
begin
Result := dateBeginOfQuarter(dateBeginOfQuarter(D)+(3*31)) - 1;
end;
// 주의 시작일
function TForm1.dateBeginOfWeek(D: TDateTime; Weekday: Integer): TDateTime;
begin
Result := D;
while DayOfWeek(Result) <> Weekday do
Result := Result - 1;
end;
// 월의 마지막 일
function TForm1.dateDaysInMonth(D: TDateTime): Integer;
const
DaysPerMonth: array[1..12] of Byte= (31,28,31,30,31,30,31,31,30,31,30,31);
var
Month: Integer;
begin
Month := dateMonth(D);
Result := DaysPerMonth[Month];
if (Month=2) and dateLeapYear(D) then
Inc(Result);
end;
간단한거..
date1 := strtodate('2001-02-01');
date2 := strtodate('2001-03-10');
ii := trunc(date2 - date1);