delphi | 델파이(Delphi)에서 날짜(Date) 관련 함수
페이지 정보
작성자 굿스웹 작성일19-01-07 14:20 조회5,028회 댓글0건관련링크
본문
// 날짜의 년도만 발취
function dateYear(D: TDateTime): Integer;
var
Year, Month, Day: Word;
begin
DecodeDate(D, Year, Month, Day);
Result := Year;
end;
// 날짜의 월만 발취
function dateMonth(D: TDateTime): Integer;
var
Year, Month, Day: Word;
begin
DecodeDate(D, Year, Month, Day);
Result := Month;
end;
// 날짜의 일만 발취
function dateDay(D: TDateTime): Integer;
var
Year,Month,Day : Word;
begin
DecodeDate (D,Year,Month,Day);
Result := Day;
end;
// 년의 시작일자
function dateBeginOfYear(D: TDateTime): TDateTime;
var
Year, Month, Day: Word;
begin
DecodeDate(D, Year, Month, Day);
Result := EncodeDate(Year, 1, 1);
end;
// 년의 마지막일자
function dateEndOfYear(D: TDateTime): TDateTime;
var
Year, Month, Day: Word;
begin
DecodeDate(D, Year, Month, Day);
Result := EncodeDate(Year, 12, 31);
end;
// 월의 시작일자
function dateBeginOfMonth(D: TDateTime): TDateTime;
var
Year, Month, Day: Word;
begin
DecodeDate(D, Year, Month, Day);
Result := EncodeDate(Year, Month, 1);
end;
// 월의 마지막일자
function 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 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 dateDayOfYear(D: TDateTime): Integer;
begin
Result := Trunc(D-dateBeginOfYear(D)) + 1;
end;
// 요일 번호
function dateDayOfWeek(D: TDateTime): TDayOfWeek;
begin
Result := TDayOfWeek(Pred(DayOfWeek(D)));
end;
function dateDayOfWeekStr(D: TDateTime): String;
var
Str:String;
begin
Str := '';
if dateDayOfWeek((D)) = Monday then Str:='월';
if dateDayOfWeek((D)) = Tuesday then Str:='화';
if dateDayOfWeek((D)) = Wednesday then Str:='수';
if dateDayOfWeek((D)) = Thursday then Str:='목';
if dateDayOfWeek((D)) = Friday then Str:='금';
if dateDayOfWeek((D)) = Saturday then Str:='토';
if dateDayOfWeek((D)) = Sunday then Str:='일';
result := Str;
end;
function dateDayOfWeekStrLong(D: TDateTime): String;
begin
Result := dateDayOfWeekStr(D)+'요일';
end;
// 윤년인지 검사
function 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 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 dateEndOfQuarter(D: TDateTime): TDateTime;
begin
Result := dateBeginOfQuarter(dateBeginOfQuarter(D)+(3*31)) - 1;
end;
// 주의 시작일
function dateBeginOfWeek(D: TDateTime; Weekday: Integer): TDateTime;
begin
Result := D;
while DayOfWeek(Result) <> Weekday do
Result := Result - 1;
end;
// 월의 마지막 일
function 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;
function dateDayCntInTerm(SD,ED: TDateTime; useToday:boolean): Integer;
var
tmstmp1, tmstmp2 : TTimeStamp;
date_gap : Integer;
begin
tmstmp1 := DateTimeToTimeStamp(SD); // TimeStamp형으로 변환
tmstmp2 := DateTimeToTimeStamp(ED); // TimeStamp형으로 변환
date_gap := tmstmp2.date - tmstmp1.date; // 사이의 날짜를 계산함.
if useToday then date_gap := date_gap+1;
result := date_gap;
end;
function dateIncDay(D: TDateTime; Inc:integer): TDateTime;
var
tmstmp : TTimeStamp;
rsDate : TdateTime;
begin
tmstmp := DateTimeToTimeStamp(D); // 지금날짜시간을 타임스템프형으로 변환
tmstmp.date := tmstmp.date + inc; // 오늘날짜에 3일을 더함..
rsDate := TimeStampToDateTime(tmstmp); // 3일 후의 날짜 출력
result := rsDate;
end;
function dateCheckInWeek(week:String; D: TDateTime):boolean;
var
i:integer;
rs : boolean;
begin
rs := false;
for i:=0 to 6 do begin
if (week[i+1] = 'o') and (dateDayOfWeek(D) = TDayOfWeek(i)) then begin
rs := true;
break;
end;
end;
result := rs;
end;
function dateYear(D: TDateTime): Integer;
var
Year, Month, Day: Word;
begin
DecodeDate(D, Year, Month, Day);
Result := Year;
end;
// 날짜의 월만 발취
function dateMonth(D: TDateTime): Integer;
var
Year, Month, Day: Word;
begin
DecodeDate(D, Year, Month, Day);
Result := Month;
end;
// 날짜의 일만 발취
function dateDay(D: TDateTime): Integer;
var
Year,Month,Day : Word;
begin
DecodeDate (D,Year,Month,Day);
Result := Day;
end;
// 년의 시작일자
function dateBeginOfYear(D: TDateTime): TDateTime;
var
Year, Month, Day: Word;
begin
DecodeDate(D, Year, Month, Day);
Result := EncodeDate(Year, 1, 1);
end;
// 년의 마지막일자
function dateEndOfYear(D: TDateTime): TDateTime;
var
Year, Month, Day: Word;
begin
DecodeDate(D, Year, Month, Day);
Result := EncodeDate(Year, 12, 31);
end;
// 월의 시작일자
function dateBeginOfMonth(D: TDateTime): TDateTime;
var
Year, Month, Day: Word;
begin
DecodeDate(D, Year, Month, Day);
Result := EncodeDate(Year, Month, 1);
end;
// 월의 마지막일자
function 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 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 dateDayOfYear(D: TDateTime): Integer;
begin
Result := Trunc(D-dateBeginOfYear(D)) + 1;
end;
// 요일 번호
function dateDayOfWeek(D: TDateTime): TDayOfWeek;
begin
Result := TDayOfWeek(Pred(DayOfWeek(D)));
end;
function dateDayOfWeekStr(D: TDateTime): String;
var
Str:String;
begin
Str := '';
if dateDayOfWeek((D)) = Monday then Str:='월';
if dateDayOfWeek((D)) = Tuesday then Str:='화';
if dateDayOfWeek((D)) = Wednesday then Str:='수';
if dateDayOfWeek((D)) = Thursday then Str:='목';
if dateDayOfWeek((D)) = Friday then Str:='금';
if dateDayOfWeek((D)) = Saturday then Str:='토';
if dateDayOfWeek((D)) = Sunday then Str:='일';
result := Str;
end;
function dateDayOfWeekStrLong(D: TDateTime): String;
begin
Result := dateDayOfWeekStr(D)+'요일';
end;
// 윤년인지 검사
function 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 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 dateEndOfQuarter(D: TDateTime): TDateTime;
begin
Result := dateBeginOfQuarter(dateBeginOfQuarter(D)+(3*31)) - 1;
end;
// 주의 시작일
function dateBeginOfWeek(D: TDateTime; Weekday: Integer): TDateTime;
begin
Result := D;
while DayOfWeek(Result) <> Weekday do
Result := Result - 1;
end;
// 월의 마지막 일
function 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;
function dateDayCntInTerm(SD,ED: TDateTime; useToday:boolean): Integer;
var
tmstmp1, tmstmp2 : TTimeStamp;
date_gap : Integer;
begin
tmstmp1 := DateTimeToTimeStamp(SD); // TimeStamp형으로 변환
tmstmp2 := DateTimeToTimeStamp(ED); // TimeStamp형으로 변환
date_gap := tmstmp2.date - tmstmp1.date; // 사이의 날짜를 계산함.
if useToday then date_gap := date_gap+1;
result := date_gap;
end;
function dateIncDay(D: TDateTime; Inc:integer): TDateTime;
var
tmstmp : TTimeStamp;
rsDate : TdateTime;
begin
tmstmp := DateTimeToTimeStamp(D); // 지금날짜시간을 타임스템프형으로 변환
tmstmp.date := tmstmp.date + inc; // 오늘날짜에 3일을 더함..
rsDate := TimeStampToDateTime(tmstmp); // 3일 후의 날짜 출력
result := rsDate;
end;
function dateCheckInWeek(week:String; D: TDateTime):boolean;
var
i:integer;
rs : boolean;
begin
rs := false;
for i:=0 to 6 do begin
if (week[i+1] = 'o') and (dateDayOfWeek(D) = TDayOfWeek(i)) then begin
rs := true;
break;
end;
end;
result := rs;
end;
[주소복사] https://www.goodsweb.kr/?u=D41Z3F6W
SNS 알리기
핀터레스트
텔레그램로
블로그로
페이스북
트위터
카카오스토리
카톡 공유
관련 블로그
로딩중