Adding/Subtracting Time

Adding/Subtracting Time

How do I add/subtract a series of time values of format hh:mm:ss (for example, 02:45:32 + 00:35:43 + and so on)?

Also, how do I set the mm:ss format?

You can use the RelativeTime function to add and subtract seconds from a time variable. To add minutes and hours, you need to use the minutes and hour function to extract the value and multiply it up to be a seconds value.

To convert a time variable into a string time in the format mm:ss, use the second form of the string function that allows you to specify an edit mask:

ls_Time = String( Now(), 'mm:ss' )

But what if the time goes past midnight?

To solve this problem, calculate the number of seconds betweenthe old time and midnight and then see whether this number of seconds is greaterthan the number of seconds you want to add to your time.

If it is, use the RelativeDate function to add a day to the date and then subtractthe number of seconds between the old time and midnight from the number of seconds you want to add, and then add those number of seconds to midnight.

If you need to work for more than 24 hours, put this in aloop until you have reduced the number of seconds to a number small enough not to roll past midnight.


Share the Post: