I'm try to create a function to Calculate age with Delphi.
ผมพยายามที่จะเขียน Code เพื่อ "คำนวนอายุจากวันเกิด"
โดยให้แสดงผลได้ ในรูปแบบปี เดือนและวัน
uses System.DateUtils;
TAge = record
AYear, AMonth, ADay, AllDays: Word;
end;
function CalculateAge(const DateOfBirth: TDate; const CheckDate: TDate): TAge;
var
NEWDOB, DOB, date2CalDay, date2CalMonth: TDate;
CurrYear, CurrMonth, CurrDay: Word;
DOBYear, DOBMonth, DOBDay, MONTHDIFF: Word;
begin
DOB := DateOfBirth;
if DOB > CheckDate then
begin
Result.AYear := 0;
Result.AMonth := 0;
Result.ADay := 0;
Result.AllDays := 0;
Exit;
end;
Result.AYear := YearsBetween(DOB, CheckDate);
Result.AllDays := DaysBetween(DateOfBirth, CheckDate);
date2CalMonth := IncYear(DOB, Result.AYear);
Result.AMonth := MonthsBetween(date2CalMonth, CheckDate);
DecodeDate(CheckDate, CurrYear, CurrMonth, CurrDay);
DecodeDate(DOB, DOBYear, DOBMonth, DOBDay);
if ((DOBMonth = CurrMonth) and (DOBDay >= CurrDay)) then
begin
date2CalDay := IncYear(DOB, Result.AYear);
Result.ADay := DaysBetween(date2CalDay, CheckDate);
end
else
begin
NEWDOB := IncYear(DOB, Result.AYear);
MONTHDIFF := MonthsBetween(NEWDOB, CheckDate);
date2CalDay := IncMonth(NEWDOB, MONTHDIFF);
Result.ADay := DaysBetween(date2CalDay, CheckDate);
end;
end;
//Sample how to calculate Age
procedure TForm2.Button1Click(Sender: TObject);
begin
with CalculateAge(DateTimePicker1.Date, DateTimePicker2.Date) do begin
self.caption:= format('%d years, %d months, %d days (all days=%d)',[ayear, amonth, aday, AllDays]);
end;
end;
Update 14/08/2017
+All days
ผมพยายามที่จะเขียน Code เพื่อ "คำนวนอายุจากวันเกิด"
โดยให้แสดงผลได้ ในรูปแบบปี เดือนและวัน
uses System.DateUtils;
TAge = record
AYear, AMonth, ADay, AllDays: Word;
end;
function CalculateAge(const DateOfBirth: TDate; const CheckDate: TDate): TAge;
var
NEWDOB, DOB, date2CalDay, date2CalMonth: TDate;
CurrYear, CurrMonth, CurrDay: Word;
DOBYear, DOBMonth, DOBDay, MONTHDIFF: Word;
begin
DOB := DateOfBirth;
if DOB > CheckDate then
begin
Result.AYear := 0;
Result.AMonth := 0;
Result.ADay := 0;
Result.AllDays := 0;
Exit;
end;
Result.AYear := YearsBetween(DOB, CheckDate);
Result.AllDays := DaysBetween(DateOfBirth, CheckDate);
date2CalMonth := IncYear(DOB, Result.AYear);
Result.AMonth := MonthsBetween(date2CalMonth, CheckDate);
DecodeDate(CheckDate, CurrYear, CurrMonth, CurrDay);
DecodeDate(DOB, DOBYear, DOBMonth, DOBDay);
if ((DOBMonth = CurrMonth) and (DOBDay >= CurrDay)) then
begin
date2CalDay := IncYear(DOB, Result.AYear);
Result.ADay := DaysBetween(date2CalDay, CheckDate);
end
else
begin
NEWDOB := IncYear(DOB, Result.AYear);
MONTHDIFF := MonthsBetween(NEWDOB, CheckDate);
date2CalDay := IncMonth(NEWDOB, MONTHDIFF);
Result.ADay := DaysBetween(date2CalDay, CheckDate);
end;
end;
//Sample how to calculate Age
procedure TForm2.Button1Click(Sender: TObject);
begin
with CalculateAge(DateTimePicker1.Date, DateTimePicker2.Date) do begin
self.caption:= format('%d years, %d months, %d days (all days=%d)',[ayear, amonth, aday, AllDays]);
end;
end;
Update 14/08/2017
+All days
Comments
Post a Comment