Delphi中的年龄计算 iAge:=2013-stroint(sJ); 如果iAge
如果你使用这个,它将只需要当前年份和用户输入的年份,并测试他是否为18岁,我正在寻找一种方法来计算用户的年龄,同时使用月份和日期,但没有任何效果,所以我希望从Stackoverflow获得一些帮助Delphi中的年龄计算 iAge:=2013-stroint(sJ); 如果iAge,delphi,Delphi,如果你使用这个,它将只需要当前年份和用户输入的年份,并测试他是否为18岁,我正在寻找一种方法来计算用户的年龄,同时使用月份和日期,但没有任何效果,所以我希望从Stackoverflow获得一些帮助 非常感谢您的帮助 思考这个问题最简单的方法是,如果你知道这个人的出生日期,你只需要知道他们的18岁生日是否已经过去 询问用户的出生日期。以日、月、年的形式获取 每年增加18个 将其转换为具有的日期 将其与今天的日期进行比较,您可以通过拨打电话找到今天的日期 代码如下所示: iAge := 2013 -
非常感谢您的帮助 思考这个问题最简单的方法是,如果你知道这个人的出生日期,你只需要知道他们的18岁生日是否已经过去
iAge := 2013 - StrToInt(sJ) ;
if iAge< 18
then
begin
bDatum := False ;
ShowMessage('You must be older than 18!') ;
Exit ;
end; //IF
if EncodeDate(dobYear + 18, dobMonth, dobDay) > Date then
ShowMessage('Too young');
现在,这几乎起作用了,但是如果这个人出生在闰日,也就是2月29日,它就会失败。你需要添加一个特殊的案例来处理这个问题。例如,粗略的方法如下:
iAge := 2013 - StrToInt(sJ) ;
if iAge< 18
then
begin
bDatum := False ;
ShowMessage('You must be older than 18!') ;
Exit ;
end; //IF
if EncodeDate(dobYear + 18, dobMonth, dobDay) > Date then
ShowMessage('Too young');
看来我刚刚在这里重新发明了轮子。总是个坏主意。您可以从
DateUtils
调用IncYear
来完成此操作,而不必担心闰日
if (dobMonth=2) and (dobDay=29) then
dobDay := 28;
if EncodeDate(dobYear + 18, dobMonth, dobDay) > Date then
ShowMessage('Too young');
考虑这一点最简单的方法是,如果你知道这个人的出生日期,你只需要知道他们的18岁生日是否已经过去
iAge := 2013 - StrToInt(sJ) ;
if iAge< 18
then
begin
bDatum := False ;
ShowMessage('You must be older than 18!') ;
Exit ;
end; //IF
if EncodeDate(dobYear + 18, dobMonth, dobDay) > Date then
ShowMessage('Too young');
现在,这几乎起作用了,但是如果这个人出生在闰日,也就是2月29日,它就会失败。你需要添加一个特殊的案例来处理这个问题。例如,粗略的方法如下:
iAge := 2013 - StrToInt(sJ) ;
if iAge< 18
then
begin
bDatum := False ;
ShowMessage('You must be older than 18!') ;
Exit ;
end; //IF
if EncodeDate(dobYear + 18, dobMonth, dobDay) > Date then
ShowMessage('Too young');
看来我刚刚在这里重新发明了轮子。总是个坏主意。您可以从
DateUtils
调用IncYear
来完成此操作,而不必担心闰日
if (dobMonth=2) and (dobDay=29) then
dobDay := 28;
if EncodeDate(dobYear + 18, dobMonth, dobDay) > Date then
ShowMessage('Too young');
考虑这一点最简单的方法是,如果你知道这个人的出生日期,你只需要知道他们的18岁生日是否已经过去
iAge := 2013 - StrToInt(sJ) ;
if iAge< 18
then
begin
bDatum := False ;
ShowMessage('You must be older than 18!') ;
Exit ;
end; //IF
if EncodeDate(dobYear + 18, dobMonth, dobDay) > Date then
ShowMessage('Too young');
现在,这几乎起作用了,但是如果这个人出生在闰日,也就是2月29日,它就会失败。你需要添加一个特殊的案例来处理这个问题。例如,粗略的方法如下:
iAge := 2013 - StrToInt(sJ) ;
if iAge< 18
then
begin
bDatum := False ;
ShowMessage('You must be older than 18!') ;
Exit ;
end; //IF
if EncodeDate(dobYear + 18, dobMonth, dobDay) > Date then
ShowMessage('Too young');
看来我刚刚在这里重新发明了轮子。总是个坏主意。您可以从
DateUtils
调用IncYear
来完成此操作,而不必担心闰日
if (dobMonth=2) and (dobDay=29) then
dobDay := 28;
if EncodeDate(dobYear + 18, dobMonth, dobDay) > Date then
ShowMessage('Too young');
考虑这一点最简单的方法是,如果你知道这个人的出生日期,你只需要知道他们的18岁生日是否已经过去
iAge := 2013 - StrToInt(sJ) ;
if iAge< 18
then
begin
bDatum := False ;
ShowMessage('You must be older than 18!') ;
Exit ;
end; //IF
if EncodeDate(dobYear + 18, dobMonth, dobDay) > Date then
ShowMessage('Too young');
现在,这几乎起作用了,但是如果这个人出生在闰日,也就是2月29日,它就会失败。你需要添加一个特殊的案例来处理这个问题。例如,粗略的方法如下:
iAge := 2013 - StrToInt(sJ) ;
if iAge< 18
then
begin
bDatum := False ;
ShowMessage('You must be older than 18!') ;
Exit ;
end; //IF
if EncodeDate(dobYear + 18, dobMonth, dobDay) > Date then
ShowMessage('Too young');
看来我刚刚在这里重新发明了轮子。总是个坏主意。您可以从
DateUtils
调用IncYear
来完成此操作,而不必担心闰日
if (dobMonth=2) and (dobDay=29) then
dobDay := 28;
if EncodeDate(dobYear + 18, dobMonth, dobDay) > Date then
ShowMessage('Too young');
Delphi将日期存储为实数-必须使用扩展类型
if IncYear(EncodeDate(dobYear, dobMonth, dobDay), 18) > Date then
ShowMessage('Too young');
Delphi将日期存储为实数-必须使用扩展类型
if IncYear(EncodeDate(dobYear, dobMonth, dobDay), 18) > Date then
ShowMessage('Too young');
Delphi将日期存储为实数-必须使用扩展类型
if IncYear(EncodeDate(dobYear, dobMonth, dobDay), 18) > Date then
ShowMessage('Too young');
Delphi将日期存储为实数-必须使用扩展类型
if IncYear(EncodeDate(dobYear, dobMonth, dobDay), 18) > Date then
ShowMessage('Too young');
我认为这更简单:
function Age(TheDate: TDate): integer;
var
I: Extended; // Extended is a special type of real variable
begin
I := Now() - TheDate; // Now() is todays date in TDate format
// The type conflict is apparently ignored
Result := round(I/365.25);
If Result > 110 then Result := 0; // this copes with a missing date string
end; // Start date in Delphi is 30/12/1899
{============================================================}
isUnder18:=年之间(DOB,Now())<18;
我认为这更简单:
function Age(TheDate: TDate): integer;
var
I: Extended; // Extended is a special type of real variable
begin
I := Now() - TheDate; // Now() is todays date in TDate format
// The type conflict is apparently ignored
Result := round(I/365.25);
If Result > 110 then Result := 0; // this copes with a missing date string
end; // Start date in Delphi is 30/12/1899
{============================================================}
isUnder18:=年之间(DOB,Now())<18;
我认为这更简单:
function Age(TheDate: TDate): integer;
var
I: Extended; // Extended is a special type of real variable
begin
I := Now() - TheDate; // Now() is todays date in TDate format
// The type conflict is apparently ignored
Result := round(I/365.25);
If Result > 110 then Result := 0; // this copes with a missing date string
end; // Start date in Delphi is 30/12/1899
{============================================================}
isUnder18:=年之间(DOB,Now())<18;
我认为这更简单:
function Age(TheDate: TDate): integer;
var
I: Extended; // Extended is a special type of real variable
begin
I := Now() - TheDate; // Now() is todays date in TDate format
// The type conflict is apparently ignored
Result := round(I/365.25);
If Result > 110 then Result := 0; // this copes with a missing date string
end; // Start date in Delphi is 30/12/1899
{============================================================}
isUnder18:=年之间(DOB,Now())<18;
我知道Stackoverflow不希望您使用“评论”部分来“+1”和“谢谢”,但我只想说,非常感谢您的快速回复和您在回答中付出的努力,为我节省了很多时间和麻烦:)@David,您确定“闰日”这件事吗?
EncodeDate
不应该处理这种情况吗?我现在明白你的意思了。在这种情况下,我会添加某种日期(18年)?好的,DateUtils
中有一个IncYear
,它应该考虑闰年(它基于SysUtils
中的IncMonth
)。虽然没有经过测试…如果你对它进行编码,然后再加上18年,你不会得到闰日的例外:IncYear(EncodeDate(dobyar,dobymonth,dobDay),18)
。我知道Stackoverflow不想让你使用注释部分“+1”和“谢谢”,但我只想说谢谢你的快速回复和你为答案付出的努力,节省了我很多时间和头痛:)@David,你确定“闰日”的事吗?EncodeDate
不应该处理这种情况吗?我现在明白你的意思了。在这种情况下,我会添加某种日期(18年)?好的,DateUtils
中有一个IncYear
,它应该考虑闰年(它基于SysUtils
中的IncMonth
)。虽然没有测试…如果你对它进行编码,那么加上18码