locked
variable date does not change when adding time [VS Ultimate 2015 Preview, Net 4.5] RRS feed

  • Question

  • I am trying to add second to date variable using timer, but for some reason, the variable does not change ... What is the reason?

        Private Time As Date = "11:55:15"
     '  Timer1.Interval = 1000
        Private Sub Timer1_Tick(sender As Object, e As EventArgs) Handles Timer1.Tick
    
            Time.AddSeconds(1)
            Console.WriteLine(Time.ToString)
    
        End Sub

    Friday, January 16, 2015 1:13 PM

Answers

  • I am trying to add second to date variable using timer, but for some reason, the variable does not change ... What is the reason?

    The .AddSeconds method is a function of the DateTime object - it returns a result.  It does not update the DateTime object.

        Private Time As Date = "11:55:15"
        Timer1.Interval = 1000
        Private Sub Timer1_Tick(sender As Object, e As EventArgs) Handles Timer1.Tick
            Time = Time.AddSeconds(1)
            Console.WriteLine(Time.ToString)
        End Sub

    See:
    http://msdn.microsoft.com/en-us/library/system.datetime.add(v=vs.110).aspx

    • Proposed as answer by Cor Ligthert Saturday, January 17, 2015 9:05 PM
    • Marked as answer by Youjun Tang Friday, January 23, 2015 8:19 AM
    Saturday, January 17, 2015 8:46 PM

All replies

  • Any Help?
    Saturday, January 17, 2015 3:13 PM
  • I am trying to add second to date variable using timer, but for some reason, the variable does not change ... What is the reason?

    The .AddSeconds method is a function of the DateTime object - it returns a result.  It does not update the DateTime object.

        Private Time As Date = "11:55:15"
        Timer1.Interval = 1000
        Private Sub Timer1_Tick(sender As Object, e As EventArgs) Handles Timer1.Tick
            Time = Time.AddSeconds(1)
            Console.WriteLine(Time.ToString)
        End Sub

    See:
    http://msdn.microsoft.com/en-us/library/system.datetime.add(v=vs.110).aspx

    • Proposed as answer by Cor Ligthert Saturday, January 17, 2015 9:05 PM
    • Marked as answer by Youjun Tang Friday, January 23, 2015 8:19 AM
    Saturday, January 17, 2015 8:46 PM