Author: Ernesto De Spirito
How can I get the dates of the first and last day of the month of a given date?
Answer:
FDOM and LDOM
FDOM returns the date that corresponds to the first day of the same month as the
date passed as parameter, while LDOM returns the date that corresponds to the last
day of the month (subtracting 1 from the date that corresponds to the first day of
the next month).
1 function FDOM(Date: TDateTime): TDateTime;
2 var
3 Year, Month, Day: Word;
4 begin
5 DecodeDate(Date, Year, Month, Day);
6 Result := EncodeDate(Year, Month, 1);
7 end;
8
9 function LDOM(Date: TDateTime): TDateTime;
10 var
11 Year, Month, Day: Word;
12 begin
13 DecodeDate(Date, Year, Month, Day);
14 if Month < 12 then
15 inc(Month)
16 else
17 begin
18 Month := 1;
19 inc(Year)
20 end;
21 Result := EncodeDate(Year, Month, 1) - 1;
22 end;
23
24 //Sample call
25
26 procedure TForm1.Button1Click(Sender: TObject);
27 begin
28 ShowMessage(DateToStr(FDOM(Now)));
29 ShowMessage(DateToStr(LDOM(Now)));
30 end;
Copyright (c) 2001 Ernesto De Spiritomailto:edspirito@latiumsoftware.com
Visit: http://www.latiumsoftware.com/delphi-newsletter.php
|