كيفية حساب عدد الساعات بين تاريخين محددين
#1
السلام عليكم
لدي برنامج تطبيقة من خلاله حساب عدد الساعات بين تارخين بمعنى
اضع تاريخ اليوم مع ساعة البدء
و اضع تاريخ الغد مع ساعة النهاية
وعندما اقوا له عد يقوم بعد عدد الساعات بين التريخين
البرنامج مرفق
مستعجل اخواني اجركم على الله
الرد
#2
كود :
var
  fromdate, toDate : TDateTime;

begin
  // Set up our date variables
  fromDate := EncodeDateTime(2000, 02, 26, 10, 0, 0, 0);
  toDate   := EncodeDateTime(2000, 02, 29,  9, 0, 0, 0);

  // Display these dates and the days between them
  ShowMessage('From date = '+DateTimeToStr(fromDate));
  ShowMessage('To   date = '+DateTimeToStr(toDate));
  ShowMessage('Whole days difference = '+
              IntToStr(DaysBetween(toDate, fromDate))+' days');
end;

إقتباس :Show full unit code
From date = 26/02/2000 10:00:00
To date = 29/02/2000 09:00:00
Whole days difference = 2 days



or
use:
DateUtils, IdGlobalProtocols

كود :
procedure TForm1.Button1Click(Sender: TObject);
var
  day, hour: Integer;
  fromdate, toDate: TDateTime;
begin
  fromdate := Date1.Date;
  toDate := Date2.Date;
  day := DaysBetween(toDate, fromdate);
  fromdate := Date1.Time;
  toDate := Date2.Time;
  hour :=  DaysBetween(toDate, fromdate) ;
  hour :=hour + day*24   ;
   heure1.Text  := IntToStr(hour)  ;
  // day := CompareDateTime(Date2.DateTime, Date1.DateTime ) ;
  // hour := CompareDateTime(Date2.Time, Date1.Time ) ;   ;
  // hour :=hour + day*24   ;

  end;
الرد
#3
كود :
procedure TMainForm.btCompareDatesClick(Sender: TObject);
var
  Result: Integer;
begin
  mmInfo.Lines.Clear;
  { Compares the two dates }
  Result := CompareDate(StrToDate(edFirstDate.Text), StrToDate(edSecondDate.Text));
  { Write the date in the memo }
  mmInfo.Lines.Add('First date: ' + edFirstDate.Text);
  mmInfo.Lines.Add('Second date: ' + edSecondDate.Text);
  { Write the result of the comparison }
  if Result = LessThanValue then
    mmInfo.Lines.Add('The first date is before the second date.');
  if Result = EqualsValue then
    mmInfo.Lines.Add('The first date is the same as the second date.');
  if Result = GreaterThanValue then
    mmInfo.Lines.Add('The first date is after the second date.');
end;

procedure TMainForm.btGetFirstDateClick(Sender: TObject);
begin
  { Write the selected date in the first edit-box }
  edFirstDate.Text := DateToStr(DateOf(DateTimePicker1.DateTime));
end;

procedure TMainForm.btGetInfoClick(Sender: TObject);
begin
  { Get the day from the selected date }
  edDay.Text := IntToStr(DayOf(DateTimePicker1.Date));
  { Get the moonth from the selected date }
  edMonth.Text := IntToStr(MonthOf(DateTimePicker1.Date));
  { Get the year from the selected date }
  edYear.Text := IntToStr(YearOf(DateTimePicker1.Date));
  { Get the hour from the selected date }
  edHour.Text := IntToStr(HourOf(TimeOf(DateTimePicker1.Time)));
  { Get the minute from the selected date }
  edMinute.Text := IntToStr(MinuteOf(TimeOf(DateTimePicker1.Time)));
  { Get the second from the selected date }
  edSecond.Text := IntToStr(SecondOf(TimeOf(DateTimePicker1.Time)));
  { Get the millisecond from the selected date }
  edMilliSecond.Text := IntToStr(MilliSecondOf(TimeOf(DateTimePicker1.Time)));
end;

procedure TMainForm.btGetSecondDateClick(Sender: TObject);
begin
  { Write the selected date in the second edit-box }
  edSecondDate.Text := DateToStr(DateOf(DateTimePicker1.DateTime));
end;

procedure TMainForm.FormCreate(Sender: TObject);
begin
  DateTimePicker1.Format := 'dd/MM/yyyy       hh:mm';
  { Get the current date and time }
  DateTimePicker1.Date := Date;
  DateTimePicker1.Time := Time;
  { Write the current date in the first edit-box }
  edFirstDate.Text := DateToStr(Today);
  { Write the previous date in the second edit-box }
  edSecondDate.Text := DateToStr(Yesterday);
end;
الرد
#4
كود :
procedure TMainForm.btCompareDatesClick(Sender: TObject);
var
  Result: Integer;
begin
  mmInfo.Lines.Clear;
  { Compares the two dates }
  Result := CompareDate(StrToDate(edFirstDate.Text), StrToDate(edSecondDate.Text));
  { Write the date in the memo }
  mmInfo.Lines.Add('First date: ' + edFirstDate.Text);
  mmInfo.Lines.Add('Second date: ' + edSecondDate.Text);
  { Write the result of the comparison }
  if Result = LessThanValue then
    mmInfo.Lines.Add('The first date is before the second date.');
  if Result = EqualsValue then
    mmInfo.Lines.Add('The first date is the same as the second date.');
  if Result = GreaterThanValue then
    mmInfo.Lines.Add('The first date is after the second date.');
end;

procedure TMainForm.btGetFirstDateClick(Sender: TObject);
begin
  { Write the selected date in the first edit-box }
  edFirstDate.Text := DateToStr(DateOf(DateTimePicker1.DateTime));
end;

procedure TMainForm.btGetInfoClick(Sender: TObject);
begin
  { Get the day from the selected date }
  edDay.Text := IntToStr(DayOf(DateTimePicker1.Date));
  { Get the moonth from the selected date }
  edMonth.Text := IntToStr(MonthOf(DateTimePicker1.Date));
  { Get the year from the selected date }
  edYear.Text := IntToStr(YearOf(DateTimePicker1.Date));
  { Get the hour from the selected date }
  edHour.Text := IntToStr(HourOf(TimeOf(DateTimePicker1.Time)));
  { Get the minute from the selected date }
  edMinute.Text := IntToStr(MinuteOf(TimeOf(DateTimePicker1.Time)));
  { Get the second from the selected date }
  edSecond.Text := IntToStr(SecondOf(TimeOf(DateTimePicker1.Time)));
  { Get the millisecond from the selected date }
  edMilliSecond.Text := IntToStr(MilliSecondOf(TimeOf(DateTimePicker1.Time)));
end;

procedure TMainForm.btGetSecondDateClick(Sender: TObject);
begin
  { Write the selected date in the second edit-box }
  edSecondDate.Text := DateToStr(DateOf(DateTimePicker1.DateTime));
end;

procedure TMainForm.FormCreate(Sender: TObject);
begin
  DateTimePicker1.Format := 'dd/MM/yyyy       hh:mm';
  { Get the current date and time }
  DateTimePicker1.Date := Date;
  DateTimePicker1.Time := Time;
  { Write the current date in the first edit-box }
  edFirstDate.Text := DateToStr(Today);
  { Write the previous date in the second edit-box }
  edSecondDate.Text := DateToStr(Yesterday);
end;
الرد
#5
إقتباس :Call CompareDate to compare the two TDateTime values specified by A and B. CompareDate returns:

LessThanValue if A occurs on a day prior to the day specified by B.
EqualsValue if A occurs on the same day as B, ignoring the time of day.
GreaterThanValue if A occurs on a day that follows the day specified by B.
كود :
procedure TMainForm.btCompareDatesClick(Sender: TObject);
var
  Result: Integer;
begin
  mmInfo.Lines.Clear;
  { Compares the two dates }
  Result := CompareDate(StrToDate(edFirstDate.Text), StrToDate(edSecondDate.Text));
  { Write the date in the memo }
  mmInfo.Lines.Add('First date: ' + edFirstDate.Text);
  mmInfo.Lines.Add('Second date: ' + edSecondDate.Text);
  { Write the result of the comparison }
  if Result = LessThanValue then
    mmInfo.Lines.Add('The first date is before the second date.');
  if Result = EqualsValue then
    mmInfo.Lines.Add('The first date is the same as the second date.');
  if Result = GreaterThanValue then
    mmInfo.Lines.Add('The first date is after the second date.');
end;

procedure TMainForm.btGetFirstDateClick(Sender: TObject);
begin
  { Write the selected date in the first edit-box }
  edFirstDate.Text := DateToStr(DateOf(DateTimePicker1.DateTime));
end;

procedure TMainForm.btGetInfoClick(Sender: TObject);
begin
  { Get the day from the selected date }
  edDay.Text := IntToStr(DayOf(DateTimePicker1.Date));
  { Get the moonth from the selected date }
  edMonth.Text := IntToStr(MonthOf(DateTimePicker1.Date));
  { Get the year from the selected date }
  edYear.Text := IntToStr(YearOf(DateTimePicker1.Date));
  { Get the hour from the selected date }
  edHour.Text := IntToStr(HourOf(TimeOf(DateTimePicker1.Time)));
  { Get the minute from the selected date }
  edMinute.Text := IntToStr(MinuteOf(TimeOf(DateTimePicker1.Time)));
  { Get the second from the selected date }
  edSecond.Text := IntToStr(SecondOf(TimeOf(DateTimePicker1.Time)));
  { Get the millisecond from the selected date }
  edMilliSecond.Text := IntToStr(MilliSecondOf(TimeOf(DateTimePicker1.Time)));
end;

procedure TMainForm.btGetSecondDateClick(Sender: TObject);
begin
  { Write the selected date in the second edit-box }
  edSecondDate.Text := DateToStr(DateOf(DateTimePicker1.DateTime));
end;

procedure TMainForm.FormCreate(Sender: TObject);
begin
  DateTimePicker1.Format := 'dd/MM/yyyy       hh:mm';
  { Get the current date and time }
  DateTimePicker1.Date := Date;
  DateTimePicker1.Time := Time;
  { Write the current date in the first edit-box }
  edFirstDate.Text := DateToStr(Today);
  { Write the previous date in the second edit-box }
  edSecondDate.Text := DateToStr(Yesterday);
end;
الرد
#6
ممكن ات تطبق الكود سورس على التطبيق الذي وصعته اخي الكريم شكرا لك على المجهود
الرد
#7
كود :
procedure TForm1.Button1Click(Sender: TObject);
var
  fromdate, toDate : TDateTime;

begin
  // Set up our date variables
  fromDate := EncodeDateTime(1899, 12, 30, 5, 09, 00, 0);// 12/30/1899 5:09:00 PM
  //
  toDate   := EncodeDateTime(2011, 08, 03,  6, 29, 05, 0);  // 8/3/2011 6:29:05 PM
  //

  // Display these dates and the days between them
  ShowMessage('From date = '+DateTimeToStr(fromDate));
  ShowMessage('To   date = '+DateTimeToStr(toDate));
  ShowMessage('Whole days difference = '+
              IntToStr(DaysBetween(toDate, fromDate))+' days');
  ShowMessage('Whole hours difference = '+
              IntToStr(HoursBetween(toDate, fromDate))+' hours');
end;
الرد
#8
السلام اخي هنا المشكل فلم يتعرف على DaysBetween
الرد
#9
لقد عرفت مشاكل كثيرة في الكود سورس
اخي الكريم ممكن ان ترفق الكود سوري في البرنامج الذي وضعته انا وكون شاكرا لك رمضان كريم صديقي
الرد
#10
عملت لك اسورس وصورة ولكن انقعت الكهرباء
حضك نحس!!
ولكن ساعمل لك مرة ثاني
الرد


التنقل السريع :


يقوم بقرائة الموضوع: بالاضافة الى ( 1 ) ضيف كريم