0

In my project I created a label to show a start time and two buttons to adjust it, one is add 15min for each click, the other is subtract 15min. The code is as follows

Label1.text = "04:30 AM"
Private Sub Btn_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Btn_Add.Click, Btn_Sub.Click
Dim btn_clicked As Button = Ctype(sender, Button)
If btn_clicked.Name = "Btn_Add" Then
    Label1.text = Ctype(Label.text, DateTime).AddMinutes(15).ToString("hh:mm tt")
ElseIf btn_clicked.Name = "Btn_Sub" Then
    Label1.text = Ctype(Label.text, DateTime).AddMinutes(-15).ToString("hh:mm tt")
End If
End Sub

The buttons are work fine at first, but if I keep clicking subtract button, I hope the time will change as a cycle, for example, 4:30 AM -> 4:15 AM ->... ->12:15 AM -> 12:00 AM -> 11:45 PM -> 11:30 PM

But once the jump mid-night event happens the exception throw out,

System.ArgumentOutOfRangeException was unhandled Message="Specified argument was out of the range of valid values."

How would that happen? The min value for datetime is 00:00:00.0000000, January 1, 0001 base on MSDN. Do I need to assign a year date as initial value?

Thanks!

apolloneo
  • 169
  • 1
  • 2
  • 18
  • You need to be mindful of the month/day/year of your DateTime value and account for that. – Douglas Barbin Oct 24 '13 at 22:03
  • 3
    You are going to hit that special moment in Bethlehem when the calendar started at 0. DateTime does not support dates before that. Best to not make your code depend on a something that happened two thousand years ago, use TimeSpan instead. Do note that it's ToString() method is quirky, check MSDN. – Hans Passant Oct 24 '13 at 22:15
  • @HansPassant - `TimeSpan` won't help here. Subtracting 15 minutes from midnight should be 11:45 PM the prior day. Using a `TimeSpan` would make it `-0:15:00`. TimeSpan is designed for elapsed duration, not for time of day. – Matt Johnson-Pint Oct 24 '13 at 22:37
  • `Label1.text = "04:30 AM"` if you printed the full date, you'd see it was `1/1/0001 4:30:00 AM` so when you get to midnight, there was no yesterday as Hans said. – Ňɏssa Pøngjǣrdenlarp Oct 24 '13 at 22:46
  • If you are not going to click the button thousands and thousands of times there are two answers that produce the results you want. What is this code for? – dbasnett Oct 24 '13 at 22:49
  • @Matt - TimeSpan is a relative value, DateTime is absolute. TimeSpan supports negative values just fine, you posted your comment "15 minutes ago". The SO software knew how to generate that string from a negative value. You didn't post 15 minutes before the year 0, no web software could/should ever handle that case. – Hans Passant Oct 24 '13 at 22:56
  • @HansPassant - I get that, and that's essentially what I said. The OP wasn't looking for "15 minutes ago". He was looking to move through absolute time of day in 15 minute increments. – Matt Johnson-Pint Oct 25 '13 at 00:50

1 Answers1

1

Try it like this

Private Sub Btn_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Btn_Add.Click, Btn_Sub.Click
    Dim btn_clicked As Button = CType(sender, Button)
    Static startTime As DateTime = #12/30/5000 4:30:00 AM#
    If btn_clicked.Name = "Btn_Add" Then
        startTime = startTime.AddMinutes(15)
        Label1.text = startTime.ToString("hh:mm tt")
    ElseIf btn_clicked.Name = "Btn_Sub" Then 'in the OP this said Btn_Add also
        startTime = startTime.AddMinutes(-15)
        Label1.text = startTime.ToString("hh:mm tt")
    End If
End Sub
dbasnett
  • 11,334
  • 2
  • 25
  • 33
  • This is pretty close, except there's a minor bug in that both branches of the if statement handle the same condition. – Matt Johnson-Pint Oct 24 '13 at 22:48
  • 1
    Yeah, I hit that one too. (Didn't see your answer earlier, so deleted mine.) Also, he may want to preload the label on form load before first button click. – Matt Johnson-Pint Oct 24 '13 at 22:51
  • Gentlemen, thanks all. This is basically for a time schedule generator, so the label there is just for showing the time in a day, doesn't care about the date. That's something else on the GUI I designed to do. PS: I also fixed the typo I made, ;-) – apolloneo Oct 25 '13 at 13:50