Delphi Pages Forums  

Go Back   Delphi Pages Forums > Delphi Forum > General

Lost Password?

Reply
 
Thread Tools Display Modes
  #1  
Old 08-23-2013, 06:39 PM
delphimpd delphimpd is offline
Senior Member
 
Join Date: Oct 2006
Posts: 701
Default Date not been calculating correctly

Hi All

And thanks in advanced for helping out.

I have this code, I had it for almost 2 year, without realizing that it has a bug when calculating the date (Not the Time)

Database is MSSQL

Here is the scenario


I have 2 fields
Both Duty_From and Duty_To (DateTime Field)

I need that when I change the time on the Duty_From field, the Duty_To field updates, based on the value of the Variable "Hours". Please note that on the code I am getting the Hours updated correctly, however the date in the Duty_To field always default to the number 24

For example if the employee's hours to work is 10 hours, and if the Duty_From field holds 08/28/2013 14:00, I get 08/24/2013 00:00

The Duty_To Hours updates correctly, but not the date.

I would appreciate your help.

I have no clue on how to get this resolve and hope you guys can help me.

Thanks

Here is the code

Code:
var
t1,t2:tdatetime;
hours : Real;
VarDuty_From : TDateTime;
begin
 Try
  VarDuty_From := 08/28/2013;

  t1 := VarDuty_From;

  hours := 10;

  t2:=t1+((1/24/60)*60*hours); //Here is where I think I have the issue



  qryEmpl_Assignment.FieldByName('DUTY_FROM').AsString     := formatDateTime('MM/DD/YYYY hh:nn',VarDuty_From);
  qryEmpl_Assignment.FieldByName('DUTY_TO').AsString       := formatDateTime('MM/DD/YYYY hh:nn',t2);
  except
    //No Error want to display
 end;
end;
Reply With Quote
  #2  
Old 08-23-2013, 07:10 PM
Jasser Jasser is online now
Moderator
 
Join Date: Jan 2005
Location: Saudi Arabia
Posts: 5,060
Default

Hi delphimpd,

First I hope you and your family are doing ok. Second I think your problem comes from the way you are incrementing the date by the number of hours. Try the following code as an example:

Code:
var
          t1, t2 : TDateTime;

          hours : Int64;
begin
          hours := 10;

          t1 := Now; //Start from any date you want.

          t2 := IncHour(t1,hours); //Increment the date by any number of hours you want.

          Showmessage(DateTimeToStr(t2)); //Show the new date.
__________________
Regards,
Abdulaziz Jasser
Reply With Quote
  #3  
Old 08-23-2013, 07:24 PM
delphimpd delphimpd is offline
Senior Member
 
Join Date: Oct 2006
Posts: 701
Default

Hi Jasser,

Thanks for your regards and I hope that your family is also doing good.

Thanks for the response. Let me try your code.

I do appreciate it.

Regards
Reply With Quote
  #4  
Old 08-26-2013, 06:02 PM
delphimpd delphimpd is offline
Senior Member
 
Join Date: Oct 2006
Posts: 701
Default

Hi Jasser,

Thanks for the help. I do appreciate it.

Your recommendation worked very well. I hope it serves to others as well.

Thank you so much my friend.

Best regards
Reply With Quote
  #5  
Old 08-26-2013, 06:11 PM
Jasser Jasser is online now
Moderator
 
Join Date: Jan 2005
Location: Saudi Arabia
Posts: 5,060
Default

I am glad to hear that...and you are very welcome my friend.
__________________
Regards,
Abdulaziz Jasser
Reply With Quote
  #6  
Old 09-17-2013, 05:22 PM
delphimpd delphimpd is offline
Senior Member
 
Join Date: Oct 2006
Posts: 701
Default

Hi There again Jasser and thanks for the help.

Sorry for going back and forth on an old post.

Using the same calculation as described before, but instead of whole number a decimal in the amount of hours such as 2.5, meaning that a person could take 2.5 hours, instead of 2

I am getting the hours from an edit field (See Value for hours), and then I convert the entry from a text to an integer and of course I get an error message saying that "2.5" is not a valid integer value.

Can you or anyone tell me what can I do to get this to work when entering a whole or a decimal value?

So this is what I do

Code:
var
          t1, t2 : TDateTime;

          hours : Int64;
begin
          hours := strToInt64(Edit1.Text);//The value here is 2.5

          t1 := Now; //Start from any date you want.

          t2 := IncHour(t1,hours); //Increment the date by any number of hours you want.

          Showmessage(DateTimeToStr(t2)); //Show the new date.
Thanks for all the help

Regards
Reply With Quote
  #7  
Old 09-17-2013, 06:01 PM
Jasser Jasser is online now
Moderator
 
Join Date: Jan 2005
Location: Saudi Arabia
Posts: 5,060
Default

Try:


Code:
var
          t1, t2 : TDateTime;

          iMinutes : Int64;
begin
          iMinutes := Trunc(StrToFloat(Edit1.Text) * 60); //Convert hours to minutes.

          t1 := Now; //Start from any date you want.

          t2 := IncMinute(t1,iMinutes); //This time it will be Minutes to increment.

          Showmessage(DateTimeToStr(t2)); //Show the new date.
__________________
Regards,
Abdulaziz Jasser
Reply With Quote
  #8  
Old 09-18-2013, 01:17 PM
delphimpd delphimpd is offline
Senior Member
 
Join Date: Oct 2006
Posts: 701
Default

Hi Jasser,

Thanks again.

That worked great.

Thanks so much

Take care

Regards,
Reply With Quote
Reply

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is On

Forum Jump


All times are GMT. The time now is 10:12 AM.


Powered by vBulletin® Version 3.8.8
Copyright ©2000 - 2019, vBulletin Solutions, Inc.