Jump to content
Sign in to follow this  
JDS2018

Date Time Differance

Recommended Posts

Hi Can you help me on below

Start Date and time : 1/1/2018 3:30:00 PM
End Date and time : 2/1/2018 4:35:00 PM

I Need to Get Result as below

Example Different Between 2 Days and Time 
1 Day 1 Hour 5 Minuts

Share this post


Link to post

Seems you are looking for TTimeSpan:

 

program Project458;

{$APPTYPE CONSOLE}

{$R *.res}

uses
  System.SysUtils,
  System.TimeSpan;

var
  span: TTimeSpan;
begin
  span := TTimeSpan.Subtract(EncodeDate(2018,1,2)+EncodeTime(16,35,0,0), EncodeDate(2018,1,1)+EncodeTime(15,30,0,0));
  Writeln(Format('%d Day(s) %d Hour(s) %d Minute(s)', [span.Days, span.Hours, span.Minutes]));
  Readln;
end.

 

Share this post


Link to post

Hi Thanks for the quick Reply

 

as per you code i have done following

 

i have add 4 DateTimePicker, 1 Label

 

As below

 

DDate1.Date  = Select Date

DTime1.Time = Select Time

 

DDate2.Date = Select Date

DTime2.Time = Select Time

 

above that how can i add it to your code ? and show the result in to label

and i am sorry i am not profeesionl for Delphi i am just learning things still.

 

If Use Code Like this 

span := TTimeSpan.Subtract(EncodeDate(DDate1.Dat)+EncodeTime(DTime1.Time), EncodeDate(DDate2.Date)+EncodeTime(DTime2.Time));

i am getting following error while i am complying

[dcc32 Error] Unit1.pas(67): E2003 Undeclared identifier: 'DDate1'

 

 

Can you help me please

 

 

 

Share this post


Link to post
Guest

Look at the types returned from EncodeDate and EncodeTime and see if there isn't any output/property with the same type in your visual controls.

Also read the documentation on those functions and the documentations on the control's properties.

I'm sure you will ba able to figure it out. Do not forget all the DatTime conversion routines in the RTL (run-time-library).

 

HTH,

 

/D

Share this post


Link to post

Hi

 

i have done following code

 

procedure TForm1.btnCalculateClick(Sender: TObject);
var
  span: TTimeSpan;
  AYear,AMonth,ADay:Word;
  BYear,BMonth,BDay:Word;
  AHour,AMinit,ASec,AMSec:Word;
  BHour,BMinit,BSec,BMSec:Word;


begin
  DecodeDate(DDate.Date,AYear,AMonth,ADay);
  DecodeTime(DTime.Time, AHour, AMinit, ASec, AMSec);

  DecodeDate(DDate2.Date,BYear,BMonth,BDay);
  DecodeTime(DTime2.Time, bHour, BMinit, BSec, BMSec);

  span := TTimeSpan.Subtract(EncodeDate(AYear,AMonth,ADay)+ EncodeTime(AHour,AMinit,ASec,AMSec),
           EncodeDate(BYear,BMonth,BDay)+ EncodeTime(BHour,BMinit,BSec, BMSec));
  Writeln(Format('%d Day(s) %d Hour(s) %d Minute(s)', [span.Days, span.Hours, span.Minutes]));
  Readln;
  ShowMessage(IntToStr(span.Days));
end;

 

But i am getting  I/O error 105

Share this post


Link to post
Guest

So turn on "Debug" and trace along to see where you are getting the error. Amend that line of code.

Is this a console project? No, it looks like a GUI project. Why writeln?

Can't you just put a TMemo on your form and output the text there?

 

Memo1.Lines.Add(Format(...));

 

Also, (after you get your stuff working), why Encode and then Decode?

 

HTH,

 

/Dany

Share this post


Link to post
28 minutes ago, Dany Marmur said:

Is this a console project? No, it looks like a GUI project. Why writeln?

He was probably mislead by my code example, which actually was a console application.

Share this post


Link to post

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
Sign in to follow this  

×