Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Wanet Telecoms Ltd on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Difference between two date and times 2

Status
Not open for further replies.

gromboy

IS-IT--Management
Mar 12, 2008
67
GB
Hi,
I have an sql server db and want to calculate the difference between two time and dates, in hours.

Field 1: {PHARMCHECKDATE} Datetime field (with no time entered)
Field 2: {PHARMCHECKTIME} String field


Field 3: {PRESC_DATE} Datetime field (with no time entered)
Field 4: {PRESC_TIME) String field


What I need to do is get the hours difference between - ({PRESC_DATE}{PRESC_TIME))-({PHARMCHECKDATE {PHARMCHECKTIME})

In the front end of the app..a user will do a Pharmacy check...then a day or few hours after the Prescriber will "Authorise" treatment. But it sometimes doesnt happen in this order...

I need the time difference between both... whether this is + or -
Any help grately appreciated as ever :)
Steve




 
What is the format of you Time String

Basically you need to create datetime strings, so that you can use the function

DTSToDateTime ("2000/01/13 11:30:15")

Assuming your dates and time as as above

@TimeDiff
datetimevar Presc;
datetimevar Pharm;

Presc:= dtstoDatetime(toext({PRESC_DATE}, 'yyyy/MM/dd')&" "&{PRESC_TIME});

Pharm:= dtstoDatetime(toext({PHARMCHECKDATE}, 'yyyy/MM/dd')&" "&{PHARMCHECKTIME});

datediff("h", Presc, Pharm):

Ian
 
Ian...that works, thanks so much
Best Wishes
Steve
 
Hi,
Continuation from the above thread back in October...

I've discovered I need the hours and minutes from this formula rather than hours only.... I've tried making changes but havent been able to do it..

This is the formula..

@TimeDiff
datetimevar Presc;
datetimevar Pharm;

Presc:= dtstoDatetime(toext({PRESC_DATE}, 'yyyy/MM/dd')&" "&{PRESC_TIME});

Pharm:= dtstoDatetime(toext({PHARMCHECKDATE}, 'yyyy/MM/dd')&" "&{PHARMCHECKTIME});

datediff("h", Presc, Pharm):

Any help greatly appreciated as ever
Steve
 
@TimeDiff
datetimevar Presc;
datetimevar Pharm;
numbervar timediff;
numbervar hour;
numbervar minutes;


Presc:= dtstoDatetime(toext({PRESC_DATE}, 'yyyy/MM/dd')&" "&{PRESC_TIME});

Pharm:= dtstoDatetime(toext({PHARMCHECKDATE}, 'yyyy/MM/dd')&" "&{PHARMCHECKTIME});

timediff:= datediff("n", Presc, Pharm);

hour:= int(timediff/60);
min:= remainder(timediff, 60);

Ian


 
Thanks Ian...
Seem to be getting an error here

min:= remainder(timediff, 60);

Remaining text does not appear to be part of the formula
Steve
 
Sorry declared var and var used are different!

Should be

minutes:= remainder(timediff, 60);
 
It seems to work for the minutes side of things but when its over one day I need it to display hours:minutes

For example
14/11/2008 14:19 vs 17/11/2008 12:42 is outputting 23

Over one hour also seem a bit odd..

can we display HH:MM ?

Steve
 
I guess we need to take the "n" from here

timediff:= datediff("n", Presc, Pharm);

and express this as a HH:MM:SS.. Not sure how to do this..

I've had a play... but no joy
Steve
 
Just convert your numbervars to text.

Last line of formula should be

totext(hour,0,"")&":"& totext(minutes,0,"")

Ian
 
My formula now reads:

datetimevar Presc;
datetimevar Pharm;
numbervar timediff;
numbervar hour;
numbervar minutes;


Presc:= dtstoDatetime(totext({PATDRUG.PRESC_DATE}, 'yyyy/MM/dd')&" "&{PATDRUG.PRESC_TIME});

Pharm:= dtstoDatetime(totext({PATDRUG.PHARMCHECKDATE}, 'yyyy/MM/dd')&" "&{PATDRUG.PHARMCHECKTIME});
minutes:= remainder(timediff, 60);
totext(hour,0,"")&":"& totext(minutes,0,"")


But I get 00:00 for every output
Steve
 
You forgot to set the value of the variable timediff.

-LB
 
Sorry.. I'm a bit thick...
What exactly do I need to do ?
Steve
 
As LB said you ahve left out the evalauation of key vars

timediff:= datediff("n", Presc, Pharm);
hour:= int(timediff/60);

YOur formula should be

datetimevar Presc;
datetimevar Pharm;
numbervar timediff;
numbervar hour;
numbervar minutes;


Presc:= dtstoDatetime(totext({PATDRUG.PRESC_DATE}, 'yyyy/MM/dd')&" "&{PATDRUG.PRESC_TIME});

Pharm:= dtstoDatetime(totext({PATDRUG.PHARMCHECKDATE}, 'yyyy/MM/dd')&" "&{PATDRUG.PHARMCHECKTIME});

timediff:= datediff("n", Presc, Pharm);
hour:= int(timediff/60);

minutes:= remainder(timediff, 60);
totext(hour,0,"")&":"& totext(minutes,0,"")

Ian

 
Hi Ian,
It works great apart from when I have less than 10 minutes between times... It then puts the value in the wrong place.

eg 7 minutes represented as 00:7 with no trailing zero...

Steve
 
Change the last line to:

totext(hour,0,"")&":"& totext(minutes,"00")

-LB
 
Thankyou to you both... this works great..
best wishes
Steve
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top