首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > .NET > .NET >

怎么计算一个人的年龄,能精确到月/天的

2012-10-19 
如何计算一个人的年龄,能精确到月/天的。最好能简单些。[解决办法]Delphi(Pascal) codeprocedure TForm1.But

如何计算一个人的年龄,能精确到月/天的。
最好能简单些。

[解决办法]

Delphi(Pascal) code
procedure TForm1.Button1Click(Sender: TObject);  function CalTwoDateDiff(vBeginDate,vEndDate: TDateTime): String;  var    iYear,iMonth,iDays,iHours,iMinutes: Integer;    str: String;  begin    iYear :=YearsBetween(vEndDate,strtodate(formatdatetime('yyyy-mm-dd',vBeginDate)));    iMonth := MonthsBetween(IncYear(vEndDate,-iYear),strtodate(formatdatetime('yyyy-mm-dd',vBeginDate)));    iDays := DaysBetween(IncMonth(vEndDate,-MonthsBetween(vEndDate,strtodate(formatdatetime('yyyy-mm-dd',vBeginDate)))),             strtodate(formatdatetime('yyyy-mm-dd',vBeginDate)));    iHours := HoursBetween(IncDay(vEndDate,-DaysBetween(vEndDate,strtodate(formatdatetime('yyyy-mm-dd',vBeginDate)))),             strtodateTime(formatdatetime('yyyy-mm-dd hh:mm:ss',vBeginDate)));    iMinutes := MinutesBetween(IncHour(vEndDate,-HoursBetween(vEndDate,strtodateTime(formatdatetime('yyyy-mm-dd hh:mm:ss',vBeginDate)))),             strtodateTime(formatdatetime('yyyy-mm-dd hh:mm:ss',vBeginDate)));    str := '';    if iYear>0 then str := str + IntToStr(iYear)+'年';    if iMonth>0 then str := str + IntToStr(iMonth)+'月';    if iDays>0 then str := str + IntToStr(iDays)+'天';    if iHours>0 then str := str + IntToStr(iHours)+'小时';    if iMinutes>0 then str := str + IntToStr(iMinutes)+'分钟';    Result := str;  end;begin  DateSeparator := '-';  showmessage('你已经活了 '+CalTwoDateDiff(datetimepicker1.datetime,now())+ ',够长的了');end; 

热点排行