Click here to Skip to main content
15,899,314 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
So this is causing me some problems, i have 2 comboxes that are populated on form load with hours for a start time and an end time to a work shift, once combo1 time and combo2 time is selected the time difference in hours should be displayed in a label, but it lets me select but only displays the hours as 00:00:00 can anyone see what i have done wrong please its probably something so simple i can no longer see, im still a vb beginner so any advice would be helpful, thanks

What I have tried:

my code so far is :

populate the combos on load
'Set the Start Time Value
Dim start As DateTime = DateTime.ParseExact("00:00", "HH:mm", Nothing)
'Set the End Time Value
Dim [end] As DateTime = DateTime.ParseExact("23:59", "HH:mm", Nothing)
'Set the interval time
Dim interval As Integer = 15
'List to hold the values of intervals
Dim lstTimeIntervals As New List(Of String)()
'Populate the list with 15 minutes interval values
Dim i As DateTime = start
While i <= [end]
ComboBox1.Items.Add(i.ToString("HH:mm tt"))
ComboBox2.Items.Add(i.ToString("HH:mm tt"))
i = i.AddMinutes(interval)
End While
Then

Private Sub ComboBox2_SelectedIndexChanged(sender As Object, e As EventArgs) Handles ComboBox2.SelectedIndexChanged

Dim duration As New TimeSpan()
Dim MyVariable1 As TimeSpan
Dim MyVariable2 As TimeSpan

MyVariable2 = ComboBox2.SelectedValue()
MyVariable1 = ComboBox1.SelectedValue()

duration = MyVariable2.Subtract(MyVariable1)

Label3.Text = duration.ToString

End Sub
Posted
Updated 5-Mar-18 12:37pm

1 solution

Firstly, name your controls with descriptive names, even if it is a test application, it takes a little more time but will always save you time in the long run.
Secondly, learn how to use a debugger, you will learn much more from debugging than pretty much anything else, the following links will help;
Debugger Basics | Microsoft Docs[^]
Navigating through Code with the Debugger[^]
In regards to your question, the following will help;
VB
' Use Date object instead of TimeSpan to get your values
Dim datStart as New Date
Dim datEnd as New Date
' Convert the values from the ComboBox
' NOTE: Selected Value will not be valid unless you set the Value member, hence use Text property
datStart = DateTime.ParseExact(ComboBox1.Text, "HH:mm tt", Nothing)
datEnd = DateTIme.ParseExact(ComboBox2.Text, "HH:mm tt", Nothing)
' A timespan is returned by arithmetic operations on DateTime objects
Dim tsDuration as New TimeSpan()
tsDuration = datEnd - datStart


Hope this helps

Kind Regards
 
Share this answer
 
Comments
Member 8365433 5-Mar-18 19:17pm    
Thanks that did the trick, however im left with another problem if someone works the 10pm to 8am shiftit says -14:00:00 when it should say 10:00:00 any suggestions

Thanks
an0ther1 5-Mar-18 22:31pm    
Your debugger would assist with this but the reason is as follows;
When you use ParseExact & pass only a time value, the date component is assumed to be the current date.
Therefore your DateTime values will be (assuming date code runs is 1st March 2018);
dateStart = 1st March 2018 2200
dateEnd = 1st March 2018 0800
A difference of -14 hours
The easiest way to resolve this would be to check if dateEnd is earlier than dateStart & if so, add a day to dateEnd.
This may cause other issues but you should be able to resolve them

Kind Regards

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900