# Hour calculation

i have 2 textboxes one for start time and one for finish time. The two text boxes are formatted as a Short Time. The third text box i want to give me the total hours. so....

9:15AM in first text box
6:00PM in the second text box

and the third text box should show 8.45 Please help....

## Comments

• [b][red]This message was edited by DrMarten at 2006-5-8 15:39:18[/red][/b][hr]

[b]
Hi,

Note that SHORT TIME is 24Hr format.
MEDIUM TIME is 12Hr format.
[/b]

[code]
Private Sub Command1_Click()

Dim t1, t2 As Date
t1 = "09:15" [green]'or t1=CDATE(text1)[/green]
t2 = "18:00" [green]'or t2=CDATE(text2)[/green]
[green]
'DEFINE STRINGS TO GET THE VALUES.[/green]
Dim t1string, t2string, t3string, t4string As String[green]
'GET TIME1 HRS.[/green]
t1string = Left(CStr(t1), 2) [green] 'or Left(CSTR(text1), 2)
'GET TIME2 HRS.[/green]
t2string = Left(CStr(t2), 2) [green] 'or Left(CSTR(text2), 2)

'GET TIME1 MINS.[/green]
t3string = Right(CStr(t1), 2)[green] 'or Right(CSTR(text1), 2)
'GET TIME2 MINS.[/green]
t4string = Right(CStr(t2), 2)[green] 'or Right(CSTR(text2), 2)
'DEFINE VARIABLES TO DO THE CALCULATIONS.[/green]
Dim num1, num2, num3, num4, hrs, mins As Integer[green]
'NEXT 3 LINES>> CONVERT THE NUMBERS AND GET THE POSITIVE RESULT FOR HOURS.[/green]
num1 = CInt(t1string)
num2 = CInt(t2string)
hrs = Abs(num2 - num1)
[green]
'NEXT 3 LINES>> CONVERT THE NUMBERS AND GET THE POSITIVE RESULT FOR MINS.[/green]
num3 = CInt(t3string)
num4 = CInt(t4string)
mins = Abs(num4 - num3)
[green]
'WRITE THE RESULT TO TEXT3.[/green]
text3 = CStr(hrs) & ":" & CStr(mins)

End Sub
[/code]

Regards,

Dr M.

[blue]
P.S. For any programming "purists" reading this.
I have used the CSTR and CINT functions as the above code is more easily converted to VB.Net and [b]it doesn't matter the if OPTION EXPLICIT or/and OPTION STRICT are ON or OFF[/b] the code will still work.
[/blue]

• Ermmm.. why not use the Split-Function?

Dim time1() as string 'Or Integer as Datatype

time1=Split(t1, ":")

--> time1(0) are the hours, time1(1) are the minutes.....

Zvoni

: [b][red]This message was edited by DrMarten at 2006-5-8 15:39:18[/red][/b][hr]
:
: [b]
: Hi,
:
: Note that SHORT TIME is 24Hr format.
: MEDIUM TIME is 12Hr format.
: [/b]
:
: [code]
: Private Sub Command1_Click()
:
: Dim t1, t2 As Date
: t1 = "09:15" [green]'or t1=CDATE(text1)[/green]
: t2 = "18:00" [green]'or t2=CDATE(text2)[/green]
: [green]
: 'DEFINE STRINGS TO GET THE VALUES.[/green]
: Dim t1string, t2string, t3string, t4string As String[green]
: 'GET TIME1 HRS.[/green]
: t1string = Left(CStr(t1), 2) [green] 'or Left(CSTR(text1), 2)
: 'GET TIME2 HRS.[/green]
: t2string = Left(CStr(t2), 2) [green] 'or Left(CSTR(text2), 2)
:
: 'GET TIME1 MINS.[/green]
: t3string = Right(CStr(t1), 2)[green] 'or Right(CSTR(text1), 2)
: 'GET TIME2 MINS.[/green]
: t4string = Right(CStr(t2), 2)[green] 'or Right(CSTR(text2), 2)
: 'DEFINE VARIABLES TO DO THE CALCULATIONS.[/green]
: Dim num1, num2, num3, num4, hrs, mins As Integer[green]
: 'NEXT 3 LINES>> CONVERT THE NUMBERS AND GET THE POSITIVE RESULT FOR HOURS.[/green]
: num1 = CInt(t1string)
: num2 = CInt(t2string)
: hrs = Abs(num2 - num1)
: [green]
: 'NEXT 3 LINES>> CONVERT THE NUMBERS AND GET THE POSITIVE RESULT FOR MINS.[/green]
: num3 = CInt(t3string)
: num4 = CInt(t4string)
: mins = Abs(num4 - num3)
: [green]
: 'WRITE THE RESULT TO TEXT3.[/green]
: text3 = CStr(hrs) & ":" & CStr(mins)
:
: End Sub
: [/code]
:
: Regards,
:
: Dr M.
:
: [blue]
: P.S. For any programming "purists" reading this.
: I have used the CSTR and CINT functions as the above code is more easily converted to VB.Net and [b]it doesn't matter the if OPTION EXPLICIT or/and OPTION STRICT are ON or OFF[/b] the code will still work.
: [/blue]
:
:
:
:

------------------------------------------
Only stupidity of mankind and the universe
are infinite, but i'm not sure concerning
the universe. A. Einstein

• [b][red]This message was edited by DrMarten at 2006-5-9 4:7:5[/red][/b][hr]
: Ermmm.. why not use the Split-Function?
:
: Dim time1() as string 'Or Integer as Datatype
:
: time1=Split(t1, ":")
:
: --> time1(0) are the hours, time1(1) are the minutes.....
:
: Zvoni
_______________________________________________________________________

Hi,

To BarKeeper. Didn't know you could use SPLIT like that. :-)
Additionally why not say Dim time1(1) as arrays DIM'ed without an upperbound take up more memory.

You should also really use convert functions such as CSTR to convert between DataTypes.
[b]
As it is good programming practice to do so.[/b]

I know VB6 and below will do this for you but VB.Net will otherwise complain under certain circumstances.
Mostly to do with OPTION STRICT or/and OPTION EXPLICIT settings.
See my comment in [blue]BLUE[/blue] at the end of this message.

I'm mostly using VB.Net. Full professional 2003 version as it is FREE
to students courtesy of Microsoft's ACADEMIC ALLIANCE.

Regards,

Dr M.
=======================================================================

: :
: : [b]
: : Hi,
: :
: : Note that SHORT TIME is 24Hr format.
: : MEDIUM TIME is 12Hr format.
: : [/b]
: :
: : [code]
: : Private Sub Command1_Click()
: :
: : Dim t1, t2 As Date
: : t1 = "09:15" [green]'or t1=CDATE(text1)[/green]
: : t2 = "18:00" [green]'or t2=CDATE(text2)[/green]
: : [green]
: : 'DEFINE STRINGS TO GET THE VALUES.[/green]
: : Dim t1string, t2string, t3string, t4string As String[green]
: : 'GET TIME1 HRS.[/green]
: : t1string = Left(CStr(t1), 2) [green] 'or Left(CSTR(text1), 2)
: : 'GET TIME2 HRS.[/green]
: : t2string = Left(CStr(t2), 2) [green] 'or Left(CSTR(text2), 2)
: :
: : 'GET TIME1 MINS.[/green]
: : t3string = Right(CStr(t1), 2)[green] 'or Right(CSTR(text1), 2)
: : 'GET TIME2 MINS.[/green]
: : t4string = Right(CStr(t2), 2)[green] 'or Right(CSTR(text2), 2)
: : 'DEFINE VARIABLES TO DO THE CALCULATIONS.[/green]
: : Dim num1, num2, num3, num4, hrs, mins As Integer[green]
: : 'NEXT 3 LINES>> CONVERT THE NUMBERS AND GET THE POSITIVE RESULT FOR HOURS.[/green]
: : num1 = CInt(t1string)
: : num2 = CInt(t2string)
: : hrs = Abs(num2 - num1)
: : [green]
: : 'NEXT 3 LINES>> CONVERT THE NUMBERS AND GET THE POSITIVE RESULT FOR MINS.[/green]
: : num3 = CInt(t3string)
: : num4 = CInt(t4string)
: : mins = Abs(num4 - num3)
: : [green]
: : 'WRITE THE RESULT TO TEXT3.[/green]
: : text3 = CStr(hrs) & ":" & CStr(mins)
: :
: : End Sub
: : [/code]
: :
: : Regards,
: :
: : Dr M.
: :
: : [blue]
: : P.S. For any programming "purists" reading this.
: : I have used the CSTR and CINT functions as the above code is more easily converted to VB.Net and [b]it doesn't matter the if OPTION EXPLICIT or/and OPTION STRICT are ON or OFF[/b] the code will still work.
: : [/blue]

• : [b][red]This message was edited by DrMarten at 2006-5-9 4:7:5[/red][/b][hr]
: : Ermmm.. why not use the Split-Function?
: :
: : Dim time1() as string 'Or Integer as Datatype
: :
: : time1=Split(t1, ":")
: :
: : --> time1(0) are the hours, time1(1) are the minutes.....
: :
: : Zvoni
: _______________________________________________________________________
:
: Hi,
:
: To BarKeeper. Didn't know you could use SPLIT like that. :-)
: Additionally why not say Dim time1(1) [red]as arrays DIM'ed without an upperbound take up more memory.[/red]
:
: [i]You should also really use convert functions such as CSTR to convert between DataTypes.[/i]
: [b]
: As it is good programming practice to do so.[/b]
:
: I know VB6 and below will do this for you [b]but VB.Net will otherwise complain under certain circumstances.[/b]
: Mostly to do with OPTION STRICT or/and OPTION EXPLICIT settings.
: See my comment in [blue]BLUE[/blue] at the end of this message.
:
: I'm mostly using VB.Net. Full professional 2003 version as it is FREE
: to students courtesy of Microsoft's ACADEMIC ALLIANCE.
:
: Regards,
:
: Dr M.
:

[b]That's why i'm still using VB6 Enterprise *grin*[/b]

[i]And yes, you're right. Good coding includes explicit converting of Datatypes.[/i]

[red]Well, yes. DIMing without bounds uses more memory, but as far as i remember, you can't use the Split-Function then.[/red]

Zvoni

P.S.: And it seems that still nobody wants to know how to get a For-Each-Functionality without using the Collection-Object... *sigh*
------------------------------------------
Only stupidity of mankind and the universe
are infinite, but i'm not sure concerning
the universe. A. Einstein

• [b][red]This message was edited by DrMarten at 2006-5-9 14:0:11[/red][/b][hr]
This is the FUNCTION.
Split Function See Also
Join Function
Requirements
Namespace: Microsoft.VisualBasic

Module: Strings

Assembly: Microsoft Visual Basic .NET Runtime (in Microsoft.VisualBasic.dll)
Returns a zero-based, one-dimensional array containing a specified number of substrings.

Function Split(
ByVal Expression As String,
Optional ByVal Delimiter As String = " ",
Optional ByVal Limit As Integer = -1,
Optional ByVal Compare As CompareMethod = CompareMethod.Binary
) As String()
Parameters
Expression
Required. String expression containing substrings and delimiters. If Expression is a zero-length string (""), the Split function returns an array of length one, containing an empty string.
Delimiter
Optional. Single character used to identify substring limits. If Delimiter is omitted, the space character (" ") is assumed to be the delimiter. If Delimiter is a zero-length string, a single-element array containing the entire Expression string is returned.
Limit
Optional. Number of substrings to be returned; the default, 1, indicates that all substrings are returned.
Compare
Optional. Numeric value indicating the comparison to use when evaluating substrings. See Settings for values.
Settings
The Compare argument can have the following values:

Constant Description
Binary Performs a binary comparison
Text Performs a textual comparison

Example
The following example demonstrates the Split function:
[code]
Dim myString As String = "Look at these!"
' Returns ["Look", "at", "these!"]
Dim myArray() As String = Split(myString)
[/code]

Requirements
Namespace: Microsoft.VisualBasic

Module: Strings

Assembly: Microsoft Visual Basic .NET Runtime (in Microsoft.VisualBasic.dll)

See Also
Join Function
=======================================================================

I was thinking of string splitting as in.>>

mystring.Split(":",1) 'to get the 1st split etc.

String.Split MethodSee Also
String Class | String Members | System Namespace | Managed Extensions for C++ Programming
Language
C#

C++

JScript

Visual Basic

Show All
Identifies the substrings in this instance that are delimited by one or more characters specified in an array, then places the substrings into a String array.

Overload List
Identifies the substrings in this instance that are delimited by one or more characters specified in an array, then places the substrings into a String array.

Supported by the .NET Compact Framework.

[Visual Basic] Overloads Public Function Split(ParamArray Char()) As String()
[C#] public string[] Split(params char[]);
[C++] public: String* Split(__wchar_t __gc[]) __gc[];
[JScript] public function Split(Char[]) : String[];
Identifies the substrings in this instance that are delimited by one or more characters specified in an array, then places the substrings into a String array. A parameter specifies the maximum number of array elements to return.

[Visual Basic] Overloads Public Function Split(Char(), Integer) As String()
[C#] public string[] Split(char[], int);
[C++] public: String* Split(__wchar_t __gc[], int) __gc[];
[JScript] public function Split(Char[], int) : String[];
Example
[Visual Basic, C#, C++] The following code example demonstrates how count affects the number of strings returned by Split.

[Visual Basic, C#, C++] Note This example shows how to use one of the overloaded versions of Split. For other examples that might be available, see the individual overload topics.
[Visual Basic]
Imports System
Imports Microsoft.VisualBasic
_
[code]
Public Class StringSplit2

Public Shared Sub Main()

Dim delimStr As String = " ,.:"
Dim delimiter As Char() = delimStr.ToCharArray()
Dim words As String = "one two,three:four."
Dim split As String() = Nothing

Console.WriteLine("The delimiters are -{0}-", delimStr)
Dim x As Integer
For x = 1 To 5
split = words.Split(delimiter, x)
Console.WriteLine(ControlChars.Cr + "count = {0,2} ..............", x)
Dim s As String
For Each s In split
Console.WriteLine("-{0}-", s)
Next s
Next x
End Sub 'Main
End Class 'StringSplit2
[/code]

I've just dicovered there are quite a few different SPLIT methods mentioned in the VB.Net help files. :-)

It got me thinking as to using the array split method on an array of strings and then further splitting those strings into more string arrays. Phew!!
If that makes sense i guess it is possible.

I've never tried it though.

Regards,

Dr M.

Sign In or Register to comment.

#### Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!