"Add 5 minutes to current time which is in string"

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • sonyayasr
    New Member
    • Feb 2015
    • 1

    "Add 5 minutes to current time which is in string"

    Hi, I have taken two textboxes, 1 shows the current time i.e system time, while in other textbox, I want to show five minutes added to the system time. How to do that?

    Code:
    Private Sub Arrival_timeTextBox_TextChanged(sender As Object, e As EventArgs) Handles Arrival_timeTextBox.TextChanged
    
            Arrival_timeTextBox.Text = DateTime.Now.ToLongTimeString()
        End Sub
    
        Private Sub DepartureTextBox_TextChanged(sender As Object, e As EventArgs) Handles DepartureTextBox.TextChanged
    
    ??
    
        End Sub
    End Class
    Last edited by Rabbit; Feb 1 '15, 05:35 PM. Reason: Please use [code] and [/code] tags when posting code or formatted data.
  • IronRazer
    New Member
    • Jan 2013
    • 83

    #2
    Hi,

    You most likely are not going to want to set the textbox`s text inside the same textbox`s TextChanged event. Depending on what you are doing it may cause an infinite loop.

    You will want to use a Date variable to assign the current date to and display that in one textbox. Then you can use the Date.AddMinutes method to add 5 minutes to that date. Then display that in the other textbox.

    Here is an example:
    Code:
        Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
            Dim CurrentDate As Date = Now
            Arrival_timeTextBox.Text = CurrentDate.ToLongTimeString()
    
            Dim FiveMinutesAdded As Date = CurrentDate.AddMinutes(5)
            DepartureTextBox.Text = FiveMinutesAdded.ToLongTimeString
        End Sub

    Comment

    • Frinavale
      Recognized Expert Expert
      • Oct 2006
      • 9749

      #3
      Use one of the DateTime.TryPar se methods to retrieve the date out of the text box and convert it into a DateTime type.

      Once you have converted/parsed the string into a DateTime type you can use the methods like the AddMinutes Method to do what you want.

      -Frinny

      Comment

      Working...