델파이 날짜구현 함수 모음들
컨텐츠 정보
- 23,686 조회
- 0 추천
- 목록
본문
날짜를 처리하는 함수 모음
unit Unit1;
interface
uses
Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms,
Dialogs,
StdCtrls;
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);
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;
end.
관련자료
-
링크