devxlogo

Adding/Subtracting Time

Adding/Subtracting Time

Question:
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?

Answer:
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.

See also  Why ChatGPT Is So Important Today
devxblackblue

About Our Editorial Process

At DevX, we’re dedicated to tech entrepreneurship. Our team closely follows industry shifts, new products, AI breakthroughs, technology trends, and funding announcements. Articles undergo thorough editing to ensure accuracy and clarity, reflecting DevX’s style and supporting entrepreneurs in the tech sphere.

See our full editorial policy.

About Our Journalist