Calculating cumulative time value with VB

Source: Internet
Author: User
Tags expression require

VB cannot store and measure cumulative time in Date/Time fields, date/time is stored at a specific point in time instead of a time span, and cannot be stored in a date/time variable for more than 24 hours. If you enter 9:30, the input is not the length of time a device is working, but a specific time, that is, 9:30. Entering 26:30,VB in a Date/Time field displays an error message that is not a valid input. VB is not unable to date/Time field of the cumulative calculation, it is stored in the form of floating point value, can be cumulative calculation, but the result is not what we hope.

The author encounters the problem of calculating accumulative time in the work, and obtains satisfactory solution by using the two short function dhctime and dhcminutes. function Dhcminutes takes the cumulative time entered as a string value as a parameter, and the returned time value is expressed in minutes, which makes it easy to sum the number of minutes for a series of time values. The number of minutes is then converted into hours and minutes with function dhctime, and the hh:mm string output is represented in the appropriate format.

The function Dhcminutes program reads as follows, takes out the hour value of the cumulative time entered as a string value multiplied by 60, then adds the minute value of the cumulative time, resulting in the number of minutes represented by the long integer.

Function dhcMinutes(strTimeAs String)AsLong
intPos=InStr(strTime,":")
strHours=Left(strTime,intPos-1)
strMinutes=Right(strTime,Len(strTime)-intPos)
dhcMinutes=Val(strHours)*60+Val(strMinutes)
End Function

The function dhctime the number of hours (integers divisible by 60) by dividing the minutes by the "\" operator as shown below. The MoD operator gets the number of minutes (the remainder that is divisible by 60). The function then formats the output into the usual time representation style.

Function dhcTime(lngMinutes As Long)As String
dhcTime=Format(lngMinutes\60,"0")&":"
&Format(lngMinutesMod60,"00")
End Function

For example, if you require 3 time lengths of 100:03, 21:15, and 0:34, you can use the following expressions:

dhcTime(dhcMinutes("100:03")
+dhcMinutes("21:15")+dhcMinutes("0:34"))

The expression result is a string value of "121:52".

If you require 4 time lengths of 11:15, 26:06, 0:89, and 112:55, you can use the following expressions:

dhcTime(dhcMinutes("11:15")+dhcMinutes("26:06")
+dhcMinutes("0:89")+dhcMinutes("112:55"))

The expression result is a string value of "151:45".

As for the time separator, this article is represented in the usual format hh:mm output, or it can be obtained with the WINDOWSAPI call, which is no longer detailed here.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.