cancel
Showing results for 
Search instead for 
Did you mean: 
Read only

Time Difference calculation

Former Member
0 Likes
664

Hi everyone ı got a problem about calculating difference between two time fields.When ı am trying Starttime-Endtime it doesn't show result by time because my time fields are integer and ı couldn't find a way to change it... I can change my time fields to AM and PM but in this case ı can't handle the Starttime-Endtime.

The formula I am using to display time in AM and PM :

StringVar sMyTime := ToText({OCLG.BeginTime}, 0, "");

IF Len(sMyTime) = 3

Then cTime(sMyTime[1] & ":" & sMyTime[2 to 3] & " " & "AM")

else IF Len(sMyTime) = 4

Then cTime(sMyTime[1 to 2] & ":" & sMyTime[3 to 4] & " " & "AM")

I NEED HELP IN ASAP!!!

View Entire Topic
DellSC
Active Contributor
0 Likes

This would be easier if you had DateTime values, as you could use DateDiff to get the difference. However, assuming that 1 - you're converting both begin time and end time to times in similar formula and 2 - you want the difference between the two in minutes, you would do something like this to get the number of minutes between the two times (it only works if the two times are in the same day):

//Start by adding leading 0's so we don't need to look at the length...
StringVar sStartTime := Right("0" + ToText({OCLG.BeginTime}, 0, ""), 4);
StringVar sEndTime := Right("0" + ToText({OCLG.EndTime}, 0, ""), 4);
NumberVar iStartMinutes := 0;
NumberVar iEndMinutes := 0;
//Convert the strings to minutes
iStartMinutes := ToNumber(Left(sStartTime, 2)) * 60 + ToNumber(Right(sStartTime, 2);
iEndMinutes := ToNumber(Left(sEndTime, 2)) * 60 + ToNumber(Right(sEndTime, 2);
//Get the difference in minutes
iEndMinutes - iStartMinutes

If you need the result as a time you could do something like this with the result of the above formula (which I'll call {@MinuteDiff}

CTime(ToText({@MinuteDiff} \ 60, 0, "") + ":" + ToText({@MinuteDiff} mod 60, 0, ""))

Note that the first part of this equation uses a backslash for "integer divide" instead of the normal forward slash that is used for division. This gives you the integer part of the division without rounding. The "mod" in the second part gives you the remainder of the integer divide as a whole number.

-Dell