delphi4arab منتديات دلفي للعرب
حصر السن وحسابه - نسخة قابلة للطباعة

+- delphi4arab منتديات دلفي للعرب (https://delphi4arab.net/forum)
+-- قسم : منتديات دلفي للعرب التعليمية (https://delphi4arab.net/forum/forumdisplay.php?fid=3)
+--- قسم : سؤال و جواب حول دلفي (https://delphi4arab.net/forum/forumdisplay.php?fid=15)
+--- الموضوع : حصر السن وحسابه (/showthread.php?tid=4688)



حصر السن وحسابه - Bachirsoft - 31-10-2012

السلام عليكم ورحمة الله تعالى وبركاته
أما بعد
لدي مشكل في برنامجي وهي
لدي تاريخ اجراء مسابقة(Date1) ، و تاريخ الميلاد (Date2)
حيث أن المترشح للمسابقة لا يجب أن يقل سنه عن 20 سنة ولا يفوق 30 سنة
المطلوب هو:
- كيف يتم حساب هذه العملية مع اعطاء مربع حوار في ان كان العمر اقل او اكبر من 20 او 30 سنة وكذا سن المترشح ؟؟؟؟؟؟؟؟؟؟؟؟؟؟؟
وكل مشكور على المحاولة والتوجيه


حصر السن وحسابه - kachwahed - 31-10-2012

وعليكم السلام ورحمة الله وبركاته
يمكنك حساب سن المترشح يوم إجراء المسابقة، ثم فحصه وفقا لشرط المشاركة
حساب سن المترشح ليس أكثر من الفرق بين تاريخين، مثال:
كود :
var BirthDate: TDate; Age: double; begin BirthDate := StrToDate('10/10/1980'); Age := Now - BirthDate; ShowMessage(FloatToStr(Trunc(age / 356)));
بشكل أفضل (كود Simon Grossenbacher ):
PHP كود :
function CalculateAge(Birthday, CurrentDate: TDate): Integer;
var
  
Month, Day, Year, CurrentYear, CurrentMonth, CurrentDay: Word;
begin
  DecodeDate
(Birthday, Year, Month, Day);
  
DecodeDate(CurrentDate, CurrentYear, CurrentMonth, CurrentDay);

  if (
Year = CurrentYear) and (Month = CurrentMonth) and (Day = CurrentDay) then
  begin
    Result 
:= 0;
  
end
  
else
  
begin
    Result 
:= CurrentYear - Year;
    if (
Month > CurrentMonth) then
      Dec
(Result)
    else
    
begin
      
if Month = CurrentMonth then
        
if (Day > CurrentDay) then
          Dec
(Result);
    
end;
  
end;
end;

procedure TForm1.Button1Click(Sender: TObject);
begin
  Label1
.Caption := Format('Your age is %d', [CalculateAge(StrToDate('01.01.1903'), Date)]);
end; 

طريقة أخرى أيضا يمكنك إنشاء قاعدة بيانات صغيرة، حيث سن المترشح حقل حسابي باستخدام SQL
PHP كود :
SELECT Employees.FirstName, Employees.LastName, Employees.BirthDate, Year(Date())-Year([BirthDate]) AS Age
FROM Employees 
بالتوفيق.


حصر السن وحسابه - Najy Ibraheem - 31-10-2012

السلام عليكم...

هناك أيضاً الدالة YearsBetween التي تحسب الفترة بالسنوات بين تاريخين.

أضف الوحدة DateUtils إلى عبارة uses لديك.

كود :
var TheAge: Integer; begin TheAge := YearsBetween(Date1, Date2); end;

نرجو الاستفادة و السلام.


حصر السن وحسابه - Bachirsoft - 01-11-2012

وعليكم السلام اخواني...
كلها اكواد مفيدة ولاكن كيف حساب السن باليوم والشهر والسنة ؟


حصر السن وحسابه - format82 - 01-11-2012

كود :
function RemainingDays(from, _to: Tdate) : integer; begin Result := Trunc(_to - from); if Result < 0 then Result := 0; end;

كود :
Label1.Caption:=IntToStr(RemainingDays(DateTimePicker1.Date, DateTimePicker2.Date));

انظر الي هذا الرابط

http://www.delphi4arab.net/forum/showthread.php?tid=1828


حصر السن وحسابه - format82 - 01-11-2012

يمكن ايضا الاستخدام الاكواد التالية

كود :
باليوم Edit1.Text := IntToStr(DaysBetween(DateTimePicker1.Date, DateTimePicker2.Date)); شهر Edit2.Text := IntToStr(MonthsBetween(DateTimePicker1.Date, DateTimePicker2.Date)); السنة Edit3.Text := IntToStr(YearsBetween(DateTimePicker1.Date, DateTimePicker2.Date));

بعد اضافة DateUtils في uses


حصر السن وحسابه - Bachirsoft - 08-11-2012

اشكركم ولاكنهذا الكود لم يعمل مع
لو تتكرم وتعملي مثال ان امكن وشكرك مسبقا