Technical Questions
 Crystal Reports Forum : Crystal Reports 9 through 2020 : Technical Questions
Message Icon Topic: Date Difference In Hours & Minutes Post Reply Post New Topic
Author Message
Dr4ke
Senior Member
Senior Member


Joined: 09 May 2014
Online Status: Offline
Posts: 209
Quote Dr4ke Replybullet Topic: Date Difference In Hours & Minutes
    Posted: 14 Oct 2014 at 11:21pm
Morning all,

This post is 3/4 a 'Tip' with a 1/4 a question to see if anybody can answer my knowledge gap. I had a requirement to be able to report on the number of Hours and Minutes between two days in a presentable format; after searching around I found a couple of formula's and, after some testing, have consolidated the approaches and added some additional bits in of my own:


NumberVar DateDifference;
StringVar TimeDifference;

DateDifference:= DateDiff('n',{@Start DateTime},{@End DateTime});

TimeDifference:= CStr(Round(DateDifference\60),"#")+' Hours '+
                 CStr(Round(DateDifference MOD 60), "00")+' Minutes';

TimeDifference;


There are two questions which I couldn't find the answer to by searching online and these are:

1) In the below bit of the formula, what is the "#" doing? As far as I can tell it's setting the decimal places to zero but I wasn't sure if the hash tag had another use?
TimeDifference:= CStr(Round(DateDifference\60),"#")


2) In the below bit of the formula, the "00" has to be used as opposed to the usual ROUND Criteria (E.g. 0, 1, 2) as it produces an error ("Bad number format string."). My assumption is that the "00" is doing the same as a "2" would; is there a reason that it has to be set as "00" though?
CStr(Round(DateDifference MOD 60), "00")



Hopefully this will help somebody and hopefully somebody can answer my outstanding two questions :-)
IP IP Logged
z9962
Senior Member
Senior Member
Avatar

Joined: 04 Jul 2012
Online Status: Offline
Posts: 161
Quote z9962 Replybullet Posted: 14 Oct 2014 at 11:56pm
1. You are correct that it is removing the decimal places, as you are getting the difference in minutes, and this is used to calculate hours, then you dont really need the secinds.
 
2. by using 00 this will retun a 0 if it is a single digit, eg 3 would return 03 not 3, which is required with time, otherwise you 2 hrs 3 min would display as 2:3 not 2:03
 
FYI cstr is the same as totext
IP IP Logged
Dr4ke
Senior Member
Senior Member


Joined: 09 May 2014
Online Status: Offline
Posts: 209
Quote Dr4ke Replybullet Posted: 15 Oct 2014 at 12:03am
1. Is there any conceivable reason that it is more beneficial to use '"#"' instead of '0' as they do the same thing?

2. Ah right ok! Would I be right in thinking that "00" is the Syntax of MOD and not ROUND as the usual ROUND Syntax (integer numbers - 1, 2 ,3 for example) don't work in this formula.
IP IP Logged
z9962
Senior Member
Senior Member
Avatar

Joined: 04 Jul 2012
Online Status: Offline
Posts: 161
Quote z9962 Replybullet Posted: 15 Oct 2014 at 12:14am
1. best practice
2. yes the 00 relates to the CStr command, as you can see it is after the close bracket for the round command.
IP IP Logged
Dr4ke
Senior Member
Senior Member


Joined: 09 May 2014
Online Status: Offline
Posts: 209
Quote Dr4ke Replybullet Posted: 15 Oct 2014 at 12:25am
That's why I couldn't find the right Syntax for the "00" and "#" as they are both Syntax for CStr ... bugger!

Thanks :-) Found the page that I needed to explain them both!!

Thank you again.
IP IP Logged
Post Reply Post New Topic
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



This page was generated in 0.029 seconds.