Technical Questions
 Crystal Reports Forum : Crystal Reports 9 through 2022 : Technical Questions
Message Icon Topic: subtracting times Post Reply Post New Topic
Author Message
LindaOSU
Newbie
Newbie
Avatar

Joined: 06 Dec 2010
Location: United States
Online Status: Offline
Posts: 9
Quote LindaOSU Replybullet Topic: subtracting times
     Posted: 27 Dec 2010 at 6:59am
I have 2 string fields that have time data but are strings.  I converted the strings to times using ctime() (11:45 is now 11:45:00AM).  One time is when a medication is supposed to be given, the second is when it actually was.  I need a report to tell me how long before or after the schedued time the drug was actually given.  How do I subtract something like 13:45:00PM - 11:30:00AM and get the answer of 2 hours and 15 minutes? Also, the medication may have been given before the scheduled time, so that would be a negtive number.
 
Thanks!
LindaOSU
IP IP Logged
blazeme182
Groupie
Groupie
Avatar

Joined: 15 Dec 2009
Online Status: Offline
Posts: 44
Quote blazeme182 Replybullet Posted: 27 Dec 2010 at 8:35am
The simple way is to use a DateDiff formula. The formula should look something like this.
 
 
DateDiff ("n",  {Time_Scheduled}, {Time_Given})
 
this should do it.
 
Please note:
 
"n" represents minutes. you can change it to "h" to show the time in hours and "s" to show the time in seconds.
 
 
IP IP Logged
blazeme182
Groupie
Groupie
Avatar

Joined: 15 Dec 2009
Online Status: Offline
Posts: 44
Quote blazeme182 Replybullet Posted: 27 Dec 2010 at 9:04am
There is another way if you want to keep it as a time field.
 
Make a new formula as such:
 
iif ({Time_Scheduled} > {Time_Given}, ctime ({Time_Scheduled} - {Time_Given}), ctime ({Time_Given} - {Time_Scheduled}))
 
This will give you a record in a time format.
 
 
Then you would make the negative values come up as red. You can do this by following the steps below.
 
1) Place your new Formula Field into the report
2) Right click on the Field
3) Select "Format Editor"
4) Click on the "Font" tab at the top
5) Next to color you will see and icon that says "x+2" click on that icon
6) Scroll to the bottom and enter the formula:   iif ({Time_Scheduled} < {Time_Given}, crBlack, crRed)
7) Save and it should work
IP IP Logged
LindaOSU
Newbie
Newbie
Avatar

Joined: 06 Dec 2010
Location: United States
Online Status: Offline
Posts: 9
Quote LindaOSU Replybullet Posted: 27 Dec 2010 at 10:11am
I tried this.  Keeps asking for dates.  I have been using an @ field because I had to convert from a string.  So instead of
 
DateDiff ("n",  {Time_Scheduled}, {Time_Given}) I had to used:
 
DateDiff ("n",  {@Scheduled}, {@Given})
 
I have dates in other fields if necesary.  But these have to be converted from strings too.
 
Ideas?
IP IP Logged
LindaOSU
Newbie
Newbie
Avatar

Joined: 06 Dec 2010
Location: United States
Online Status: Offline
Posts: 9
Quote LindaOSU Replybullet Posted: 27 Dec 2010 at 10:14am
I tried this too.
 
iif ({@Scheduled} > {@Given}, ctime ({@Scheduled} - {@Given}), ctime ({@Given} - {@Scheduled}))
 
All answers came up as 12:00:00AM
 
I changed this to
 
then divided the answer by 60 to get minutes.


Edited by LindaOSU - 27 Dec 2010 at 12:14pm
IP IP Logged
blazeme182
Groupie
Groupie
Avatar

Joined: 15 Dec 2009
Online Status: Offline
Posts: 44
Quote blazeme182 Replybullet Posted: 28 Dec 2010 at 5:25am
what is the exact formula in the {@Scheduled} and {@Given} fields.
IP IP Logged
Printable version Printable version

Forum Jump
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot delete your posts in this forum
You cannot edit your posts in this forum
You cannot create polls in this forum
You cannot vote in polls in this forum