现要计算年龄要求精确到日,例如出生年月日为“1980-08-20”和“1980-08-25” 按今天为基准的话前者为24岁,后者为23岁,怎么在delphi中实现,谢谢了!!

解决方案 »

  1.   

    uses DateUtils;
      Edit1.Text := IntToStr(YearsBetween(now, strToDate('1980/08/20')));
      Edit2.Text := IntToStr(YearsBetween(now, strToDate('1980/08/27')));
      

  2.   

    还有个笨点方法,调试通过procedure TForm1.Button1Click(Sender: TObject);
    var
     Year_now, Month_now, Day_now: Word;
     Year_bri, Month_bri, Day_bri: Word;
     Brithday: string;
    begin
      Brithday := '1980-08-20';
      DecodeDate(Date, Year_now, Month_now, Day_now);  //分解日期为年、月、日
      DecodeDate(StrToDateTime(Brithday), Year_bri, Month_bri, Day_bri);
      if Year_now >= Year_bri then
      begin
         if Year_now = Year_bri then ShowMessage('今年刚出生');
         if Month_bri > Month_now then
           ShowMessage('年龄为'+ IntToStr(Year_now - Year_bri - 1) + '岁')
         else begin
           ShowMessage('年龄为'+ IntToStr(Year_now - Year_bri) + '岁');
         end;
      end
      else ShowMessage('无法计算年龄');
    end;
      

  3.   

    P.S. 指出aiirii老大一点小小的问题 ShowMessage(IntToStr(YearsBetween(now, strToDate('1980-08-20'))));