-2

I have to code a program which finds the elapsed time when i enter the 2 times into 2 textboxes, one textbox will be the start time, the other textbox will be the end time, I am lost as to how I do this.

Example
Start time is 12:45
the end time is 13:15
then the elapsed time should be 30 minutes


Public Class Form1 

    Dim starttime As DateTime 
    Dim endtime As DateTime 
    Dim timetaken As TimeSpan 
    Private Sub btnOK_Click(sender As Object, 
        e As EventArgs) Handles btnOK.Click 
        starttime = txtStart.Text 
        endtime = txtEnd.Text 
    End Sub 

End Class
Ňɏssa Pøngjǣrdenlarp
  • 38,411
  • 12
  • 59
  • 178
  • 1
    we cant possibly tell you whats wrong with your code without your code – Ňɏssa Pøngjǣrdenlarp Feb 11 '15 at 14:15
  • i dont have much code yet, im trying to figure out how to actually do it Public Class Form1 Dim starttime As DateTime Dim endtime As DateTime Dim timetaken As TimeSpan Private Sub btnOK_Click(sender As Object, e As EventArgs) Handles btnOK.Click starttime = txtStart.Text endtime = txtEnd.Text End Sub End Class that is all i have gotten so far, I dont know where to go from here – RobertMcGugian Feb 11 '15 at 14:16
  • 1
    Start by using `Option Strict On` (always). If `starttime` is a `DateTime` type, you cannot simply assign a string to it (`txtBox.Text`) and have it magically convert to `DateTime` – Ňɏssa Pøngjǣrdenlarp Feb 11 '15 at 14:21
  • ok, ill try this and hope for the best – RobertMcGugian Feb 11 '15 at 15:07

2 Answers2

2

Quickly out of my head:

Option Strict On 'every good programmer does this

Public Class Form1 

    Private starttime As DateTime 'Please use Dim only in functions or subs
    Private endtime As DateTime 
    Private timetaken As TimeSpan 

    Private Sub btnOK_Click(sender As Object, e As EventArgs) Handles btnOK.Click 
        starttime = DateTime.Parse(txtStart.Text) 'Parse the string input
        endtime = DateTime.Parse(txtEnd.Text)
        timetaken = endtime - starttime
    End Sub 

End Class

Of course this highly depends on what string can be parsed into a DateTime instance. It even depends on your systems culture. Have a look at https://msdn.microsoft.com/en-us/library/System.DateTime.Parse(v=vs.110).aspx for more details about how an input string should look like. If days would be enough, you could use a DatePicker control instead (which however sadly doesn't support your need for times).

You can supply a format of the input string with DateTime.ParseExact afaik

To catch errors when a string input in the textboxes could not be parsed, use DateTime.TryParse.

Ray
  • 7,940
  • 7
  • 58
  • 90
  • so how would i get this to display in a message box? I get an error when i do msgbox(timetaken) – RobertMcGugian Feb 11 '15 at 15:14
  • MessageBox requires a string, use the `.ToString()`, which you can call on any object, since it's a method of the `object` class, from which every .NET object inherits. Good classes override it give you a useful string out of it then (though not all do, then just the type name is returned, as implemented in `object` directly). – Ray Feb 11 '15 at 21:13
0

First, use Dateime.Parse or DateTime.ParseExact to convert the string in the text boxes into DateTimes,

Dim start = DateTime.Parse(txtStart.Text)

Then you can find the difference between two DateTimes by using the Subtract method which returns a TimeSpan.

Dim difference = end.Subtract(start)
Jodrell
  • 34,946
  • 5
  • 87
  • 124