none
Time difference, code lost RRS feed

  • Question

  • I lost the project with a time span en calculate the outcome 

    Found this , but i it not working.

    Textbox 1 is (14-06-2018 01:45)

    Textbox 2 is (14-06-2018 06:25)

    What am i missing to calculate the time difference between textbox1 and textbox 2 in hours and minutes . Outcome in textbox 3

    This is all i found , but not complete

    Private Function GetElapsed(ByVal t1 As String, ByVal t2 As String) As TimeSpan
            Dim time1, time2 As DateTime
    
            If Not DateTime.TryParse(t1, time1) Then Throw New ArgumentException(t1 & " is not a valid DateTime", "t1")
            If Not DateTime.TryParse(t2, time2) Then Throw New ArgumentException(t2 & " is not a valid DateTime", "t2")
            Return time2 - time1
        End Function
    
    
    
       
        Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
            Dim elapsed As New TimeSpan
            elapsed += GetElapsed(TextBox1.Text, TextBox2.Text)
             
    
            TextBox3.Text = Math.Floor(elapsed.TotalHours).ToString & ":" & elapsed.ToString("mm")
    

    Wednesday, June 13, 2018 12:05 PM

Answers

  • I lost the project with a time span en calculate the outcome 

    Found this , but i it not working.

    Textbox 1 is (14-06-2018 01:45)

    Textbox 2 is (14-06-2018 06:25)


    Hi

    Here is an example. This example caters for any days in the result to be included as 24 hours.

    ' Form1 with TextBox1,
    ' TextBox2, TextBox3
    ' and Button1
    Option Strict On
    Option Explicit On
    Public Class Form1
      Button1.Click
        ' first convert TB's to DateTimes
    
        ' convert TextBox1
        Dim dt1 As DateTime = Nothing
        If DateTime.TryParse(TextBox1.Text, dt1) Then
          ' valid DateTime
        Else
          MessageBox.Show("TextBox1 - Invalid DateTime")
          Exit Sub
        End If
    
        ' convert TextBox2
        Dim dt2 As DateTime = Nothing
        If DateTime.TryParse(TextBox2.Text, dt2) Then
          ' valid DateTime
        Else
          MessageBox.Show("TextBox2 - Invalid DateTime")
          Exit Sub
        End If
    
        ' reached here with 2
        ' valid DateTimes
        Dim diff As TimeSpan = dt2.Subtract(dt1)
    
        ' display Hours Min (including
        ' any Days converted to Hours)
        TextBox3.Text = (diff.Days * 24 + diff.Hours).ToString & "h  " & diff.Minutes.ToString & "m"
      End Sub


    Regards Les, Livingston, Scotland

    • Marked as answer by KeesBlunder Wednesday, June 13, 2018 1:30 PM
    Wednesday, June 13, 2018 12:45 PM

All replies

  • I would suggest looking at the following for a base idea.

    https://stackoverflow.com/questions/17765319/date-and-time-difference-in-minutes?utm_medium=organic&utm_source=google_rich_qa&utm_campaign=google_rich_qa


    Please remember to mark the replies as answers if they help and unmark them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.
    VB Forums - moderator
    profile for Karen Payne on Stack Exchange, a network of free, community-driven Q&A sites

    Wednesday, June 13, 2018 12:08 PM
    Moderator
  • I lost the project with a time span en calculate the outcome 

    Found this , but i it not working.

    Textbox 1 is (14-06-2018 01:45)

    Textbox 2 is (14-06-2018 06:25)


    Hi

    Here is an example. This example caters for any days in the result to be included as 24 hours.

    ' Form1 with TextBox1,
    ' TextBox2, TextBox3
    ' and Button1
    Option Strict On
    Option Explicit On
    Public Class Form1
      Button1.Click
        ' first convert TB's to DateTimes
    
        ' convert TextBox1
        Dim dt1 As DateTime = Nothing
        If DateTime.TryParse(TextBox1.Text, dt1) Then
          ' valid DateTime
        Else
          MessageBox.Show("TextBox1 - Invalid DateTime")
          Exit Sub
        End If
    
        ' convert TextBox2
        Dim dt2 As DateTime = Nothing
        If DateTime.TryParse(TextBox2.Text, dt2) Then
          ' valid DateTime
        Else
          MessageBox.Show("TextBox2 - Invalid DateTime")
          Exit Sub
        End If
    
        ' reached here with 2
        ' valid DateTimes
        Dim diff As TimeSpan = dt2.Subtract(dt1)
    
        ' display Hours Min (including
        ' any Days converted to Hours)
        TextBox3.Text = (diff.Days * 24 + diff.Hours).ToString & "h  " & diff.Minutes.ToString & "m"
      End Sub


    Regards Les, Livingston, Scotland

    • Marked as answer by KeesBlunder Wednesday, June 13, 2018 1:30 PM
    Wednesday, June 13, 2018 12:45 PM
  • Thanks Les , that works.

    When i do this several time's with other textbox's , how do i calculate sum of all the hours and min ? .

    Wednesday, June 13, 2018 1:04 PM
  • Thanks Les , that works.

    When i do this several time's with other textbox's , how do i calculate sum of all the hours and min ? .

    Hi

    You should close this question first (mark any replies as Answer/Vote up if applicable), and ask a new question - I will work on an example for that.


    Regards Les, Livingston, Scotland

    Wednesday, June 13, 2018 1:18 PM
  • Hello Les,

    I am using the code you give me , but one more question.

    The outcome is sometimes for example

    6:3

    what do i have to change that the outcome is 06:30 ?

    TextBox3.Text = (diff.Days * 24 + diff.Hours).ToString & "h  " & diff.Minutes.ToString & "m"


    in some other code i use this , but i can't combine it.

    TextBox5.Text = String.Format("{0:00}", hour) & ":" & String.Format("{0:00}", minute)


    Saturday, June 16, 2018 2:03 PM
  • Hello Les,

    I am using the code you give me , but one more question.

    The outcome is sometimes for example

    6:3

    what do i have to change that the outcome is 06:30 ?

    TextBox3.Text = (diff.Days * 24 + diff.Hours).ToString & "h  " & diff.Minutes.ToString & "m"


    in some other code i use this , but i can't combine it.

    TextBox5.Text = String.Format("{0:00}", hour) & ":" & String.Format("{0:00}", minute)


    Hi

    I can't reproduce your results - what are the values of Hour and Minute in your case when you get the 6:3 output?

    Here is the original code I posted with the output changed to your example and it works as expected here - it outputs 6:30 for 6 hour 30 minutes and 6:03 for 6 hours 3 minutes.

    ' Form1 with TextBox1,
    ' TextBox2, TextBox3
    ' and Button1
    Option Strict On
    Option Explicit On
    Public Class Form1
      Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        TextBox1.Text = Now.ToString
        TextBox2.Text = Now.AddHours(6).AddMinutes(3).ToString
    
      End Sub
      Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
        ' first convert TB's to DateTimes
    
        ' convert TextBox1
        Dim dt1 As DateTime = Nothing
        If DateTime.TryParse(TextBox1.Text, dt1) Then
          ' valid DateTime
        Else
          MessageBox.Show("TextBox1 - Invalid DateTime")
          Exit Sub
        End If
    
        ' convert TextBox2
        Dim dt2 As DateTime = Nothing
        If DateTime.TryParse(TextBox2.Text, dt2) Then
          ' valid DateTime
        Else
          MessageBox.Show("TextBox2 - Invalid DateTime")
          Exit Sub
        End If
    
        ' reached here with 2
        ' valid DateTimes
        Dim diff As TimeSpan = dt2.Subtract(dt1)
    
        ' display Hours Min (including
        ' any Days converted to Hours)
    
        '   TextBox3.Text = (diff.Days * 24 + diff.Hours).ToString & "h  " & diff.Minutes.ToString & "m"
    
        ' using your example
        TextBox3.Text = String.Format("{0:00}", (diff.Days * 24 + diff.Hours)) & ":" & String.Format("{0:00}", diff.Minutes)
      End Sub
    End Class
    


    Regards Les, Livingston, Scotland


    • Edited by leshay Saturday, June 16, 2018 2:23 PM
    Saturday, June 16, 2018 2:23 PM
  • Thanks Leshay,
    Sunday, June 17, 2018 1:32 PM