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

Reputation: 112 Stalwart
Group:
Contributors
Active Posts:
615 (0.26 per day)
Joined:
01-October 08
Profile Views:
8,326
Last Active:
User is offline Yesterday, 07:51 AM
Currently:
Offline

Previous Fields

Country:
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:
50

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
    
            stpw.Reset()
            stpw.Start()
            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)
                Next
            Next
            stpw.Stop()
            Debug.WriteLine(stpw.ElapsedTicks.ToString("n0"))
            Debug.WriteLine(stpw.ElapsedMilliseconds.ToString("n0"))
    
    


    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?
                'yes
                Return d.AddDays(8 - d.DayOfWeek)
            Else
                'sun
                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
            Do
                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:

    Hi,

    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:

            Debug.WriteLine("")
            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)
                Else
                    nxtMon = currDate.AddDays(1)
                End If
                '<< end find next monday >>
    
                Debug.WriteLine(nxtMon.ToShortDateString & " " & nxtMon.DayOfWeek.ToString)
            Next
    
    


    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
                'stop
                Timer1.Stop()
                stpw.Stop()
            Else
                'start
                Timer1.Start()
                stpw.Restart()
            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"
                    Timer1.Stop()
                    stpw.Stop()
                Else
                    Label1.Text = tmrmng.ToString("hh\:mm\:ss")
                End If
            End If
        End Sub
    
    

My Information

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

Contact Information

E-mail:
Private
Website URL:
Website URL  http://

Friends

Comments

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