求年龄(100分)

N

nbyyslf

Unregistered / Unconfirmed
GUEST, unregistred user!
请问各位,有什么函数可以做,比如说出生年月是1979-12-12 与现在的年月日比,
输出这个人今天是几岁~!应该怎么做,请告诉我,谢谢!
 
引用单元DateUtils的函数function YearsBetween(const ANow, AThen: TDateTime): Integer;
 
用procedure DecodeDate(Date: TDateTime; var Year, Month, Day: Word);
分界出年月日,自己算,还可用
procedure DecodeTime(Time: TDateTime; var Hour, Min, Sec, MSec: Word);
竟确到秒;
 
单元DateUtils的函数function YearsBetween(const ANow, AThen: TDateTime): Integer
请提供该函数的详细范例;谢谢
 
如果不行,用字符函数,取出前四位,再进行字符转换,然后计算。。
最土的办法了。。。
 
procedure TForm1.Button1Click(Sender: TObject);
var
y,m,d: word;
s: string;
begin
decodedate(now,y,m,d);
s:= copy('1979-12-12',1,4);
y := y-strtoint(s);
showmessage(inttostr(y)+'岁');
end;
 
在Delphi6中写的

Unit1.dfm的代码:
object Form1: TForm1
Left = 192
Top = 107
Width = 696
Height = 480
Caption = 'Form1'
Color = clBtnFace
Font.Charset = DEFAULT_CHARSET
Font.Color = clWindowText
Font.Height = -11
Font.Name = 'MS Sans Serif'
Font.Style = []
OldCreateOrder = False
PixelsPerInch = 96
TextHeight = 13
object Label1: TLabel
Left = 56
Top = 28
Width = 60
Height = 13
Caption = #20986#29983#26085#26399#65306
end
object DateTimePicker1: TDateTimePicker
Left = 52
Top = 52
Width = 137
Height = 21
CalAlignment = dtaLeft
Date = 37598
Time = 37598
DateFormat = dfShort
DateMode = dmComboBox
Kind = dtkDate
ParseInput = False
TabOrder = 0
end
object Button1: TButton
Left = 220
Top = 52
Width = 75
Height = 25
Caption = #24180#40836
TabOrder = 1
OnClick = Button1Click
end
end

Unit1.pas的代码:
unit Unit1;

interface

uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, ComCtrls, StdCtrls;

type
TForm1 = class(TForm)
DateTimePicker1: TDateTimePicker;
Button1: TButton;
Label1: TLabel;
procedure Button1Click(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;

var
Form1: TForm1;

implementation

{$R *.dfm}
uses DateUtils; //要增加的引用单元。

procedure TForm1.Button1Click(Sender: TObject);
var
TempInt:Integer;
begin
TempInt:=YearsBetween(Now,DateTimePicker1.DateTime);
ShowMessage('现在'+IntToStr(TempInt)+'岁了!');
end;

end.

Project1.dpr的代码:
program Project1;

uses
Forms,
Unit1 in 'Unit1.pas' {Form1};

{$R *.res}

begin
Application.Initialize;
Application.CreateForm(TForm1, Form1);
Application.Run;
end.
 
该给我加分了,怎么没有动静?
 
日期可以直接相减,再取整就可以了
 
周岁:int(now-strtoDateTime('1979-12-12'))
虚岁:int(now-strtoDateTime('1979-12-12')+0.999)
 
如果数据库是SQL SERVER
这样试试,89不离10了
SELECT SUM(DATEDIFF(year, 出生年月, GETDATE()) + 1)/
(Select Count(*) From bbb) AS aa
FROM bbb

上面结果如果在南方的话,还要加1吧.
 
谢谢各位的热心帮助
 
顶部