dbasnett's Profile User Rating: -----

Reputation: 112 Stalwart
Active Posts:
615 (0.26 per day)
01-October 08
Profile Views:
Last Active:
User is offline Today, 05:19 AM

Previous Fields

Who Cares
OS Preference:
Who Cares
Favorite Browser:
Who Cares
Favorite Processor:
Who Cares
Favorite Gaming Platform:
Who Cares
Your Car:
Who Cares
Dream Kudos:

Latest Visitors

Icon   dbasnett has not set their status

Posts I've Made

  1. In Topic: Finding Date for Next Monday

    Posted 8 Feb 2015

    I made a comment about performance earlier so I create a test to check the three differing versions. Again, performance is only a concern if this function is needed a lot.

    Here is the code used to test:
            Const tries As Integer = 100000
            Dim testdts As New List(Of DateTime) From {#2/1/2015#, #2/2/2015#, #2/3/2015#, #2/4/2015#, #2/5/2015#, #2/6/2015#, #2/7/2015#}
            Dim stpw As New Stopwatch
            For x As Integer = 1 To tries
                For d As Integer = 0 To testdts.Count - 1
                    'uncomment one of the following three Dim statements
                    'Dim nxtMon As DateTime = nextMondayIF(testdts(d))
                    'Dim nxtMon As DateTime = nextMondayOneLine(testdts(d))
                    'Dim nxtMon As DateTime = nextMondayLoop(testdts(d))
                    'for checking results
                    'If x = 1 Then Debug.WriteLine("{0} {1} {2} {3}", d, d.DayOfWeek, nxtMon, nxtMon.DayOfWeek)

    Here are the three next Monday methods:

        Private Function nextMondayIF(d As DateTime) As DateTime
            If d.DayOfWeek >= DayOfWeek.Monday Then 'is it mon - sat?
                Return d.AddDays(8 - d.DayOfWeek)
                Return d.AddDays(1)
            End If
        End Function
        Private Function nextMondayOneLine(d As DateTime) As DateTime
            Return d.AddDays(If(d.DayOfWeek = DayOfWeek.Monday, 7, (7 - d.DayOfWeek + 1) Mod 7))
        End Function
        Private Function nextMondayLoop(d As DateTime) As DateTime
                d = d.AddDays(1)
            Loop Until d.DayOfWeek = DayOfWeek.Monday
            Return d
        End Function

    The simple if and the one liner were close, and the loop took about twice as long as the other two.
  2. In Topic: Finding Date for Next Monday

    Posted 7 Feb 2015

    View Postandrewsw, on 07 February 2015 - 04:30 PM, said:

    It is possible to use DayOfWeek:
    For x As Integer = 0 To 30
    	Dim day = DateTime.Now.AddDays(x)
    	dim mon = day.AddDays((7 - day.DayOfWeek + 1) Mod 7)
    	Debug.Print(day & " " & mon)
    Next x

    This assumes that a Monday will return the same day, not the following Monday.

    So the expression to get the following Monday is:
    Dim mon = day.AddDays((7 - day.DayOfWeek + 1) Mod 7)

    I assumed next Monday was the next Monday even if it was Monday. If on the 9th of this month I told you something was going to happen next Monday I wouldn't mean the 9th. Would you think I did? Of course we don't know what the OP intends.
  3. In Topic: Finding Date for Next Monday

    Posted 7 Feb 2015

    View PostIronRazer, on 07 February 2015 - 11:20 AM, said:


    Here is another way to do it taking advantage of the DayOfWeek enum. You subtract your current date`s DayOfWeek from 7 and then just add the DayOfWeek you want to find the next one of to that, DayOfWeek.Monday in this case. Then use the Date.AddDays to add that to the current date.

            Dim dt As Date = Now
            Dim nextMonday As Date = dt.AddDays(7 - dt.DayOfWeek + DayOfWeek.Monday)
            Me.Text = nextMonday.ToLongDateString

    Doesn't work for Sunday's, it is a week off.
  4. In Topic: Finding Date for Next Monday

    Posted 7 Feb 2015

    View Postdschwartz, on 06 February 2015 - 10:48 AM, said:

    Having some trouble calculating the date of the next Monday coming up for the current date. Example: If today is 2/6/15, I need it to populate 2/9/15. It seems like there should be an easy way to do this, but I'm having trouble finding it. Everything I use either gets the Monday of the current week (i.e. for 2/6/15, it populates 2/2/15) or another day altogether. Any help is appreciated.

    The loop answer is one approach. You can also use a simple if - then - else as illustrated here:

            Dim d As DateTime = DateTime.Now ' #2/6/2015# '
            For x As Integer = 0 To 6
                Dim currDate As DateTime = d.AddDays(x)
                Debug.Write(currDate.ToShortDateString & " " & currDate.DayOfWeek.ToString & " ")
                '<< find next monday >>
                Dim nxtMon As DateTime
                If currDate.DayOfWeek >= DayOfWeek.Monday Then
                    nxtMon = currDate.AddDays(8 - currDate.DayOfWeek)
                    nxtMon = currDate.AddDays(1)
                End If
                '<< end find next monday >>
                Debug.WriteLine(nxtMon.ToShortDateString & " " & nxtMon.DayOfWeek.ToString)

    The difference is negligible from a performance point of view so long as the number of times you want to find the next Monday is small. If this is to be used a lot then the loop will not perform as well.
  5. In Topic: How can I make a countdown?

    Posted 1 Feb 2015

    Here is a way that is not dependent on the timer interval

        Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
            'start / stop timer
            If stpw.IsRunning Then
            End If
        End Sub
        Dim stpw As New Stopwatch
        Dim howlong As New TimeSpan(0, 0, 5) 'five seconds, for 21 seconds (0, 0, 21)
        Private Sub Timer1_Tick(sender As Object, e As EventArgs) Handles Timer1.Tick
            If stpw.IsRunning Then
                Dim tmrmng As TimeSpan = howlong - stpw.Elapsed 'remaining time
                If tmrmng.TotalMilliseconds <= 1001 Then
                    Label1.Text = "blast off"
                    Label1.Text = tmrmng.ToString("hh\:mm\:ss")
                End If
            End If
        End Sub

My Information

Member Title:
D.I.C Addict
Age Unknown
July 29
Years Programming:

Contact Information

Website URL:
Website URL  http://



dbasnett has no profile comments yet. Why not say hello?