CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 8 of 8
  1. #1
    Join Date
    Jan 2006
    Posts
    326

    Get last Saturday and last Sunday

    Thanks

  2. #2
    Join Date
    Jul 2001
    Location
    Sunny South Africa
    Posts
    11,283

    Re: Get last Saturday and last Sunday

    Please read here on how to properly post your question(s) :
    http://www.codeguru.com/forum/showthread.php?t=403073

    What you can do is something like this :
    Code:
        Private Function DoDate(ByVal aDate As Date, _
                                       ByVal aNumber As Integer, _
                                       ByVal aDayOfWeek As DayOfWeek, _
                                       ByVal aMonth As Integer) As Boolean
            Dim Counter As Integer = 0 'counter to hold value of days that = dayofweek
            For I As Integer = 1 To aDate.Day
                'new date to check dayofweek
                Dim NewDate As Date = New Date(aDate.Year, aMonth, I)
                If NewDate.DayOfWeek = aDayOfWeek Then
                    Counter += 1 'dayofweek = what we are looking to check
                End If
            Next
            If Counter = aNumber Then
                Return True
            Else
                Return False
            End If
        End Function
    
    
        Private Sub Form1_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
            MessageBox.Show(DoDate(Date.Now, 4, DayOfWeek.Saturday, 11))
        End Sub
    If you were to set your computer's clock to 29 November 2008, you will get True if not, you will get False.
    So, what happense here is that it counts every occurence of Saturday ( or whichever day you specified ), then, it looks at the current computer time, see if it is the 4th occurence of Saturday in November

    I had to do something similar, in one of my projects, where I had to determine where Fridays & Mondays are, and how many of them in a specified time.

    I hope my post was useful

  3. #3
    Join Date
    Jan 2006
    Posts
    326

    Re: Get last Saturday and last Sunday

    Thank you very much.
    If I only want to get last Saturday based on Today, how to get it?
    Today is a variable.

  4. #4
    Join Date
    Apr 2008
    Posts
    82

    Re: Get last Saturday and last Sunday

    what if today is Sunday. is yesterday "last" saturday?

  5. #5
    Join Date
    Jan 2003
    Location
    7,107 Islands
    Posts
    2,487

    Re: Get last Saturday and last Sunday

    last sat/sun of the what? i think you need to be specific.
    Busy

  6. #6
    Join Date
    Jul 2001
    Location
    Sunny South Africa
    Posts
    11,283

    Re: Get last Saturday and last Sunday

    Quote Originally Posted by Oblio View Post
    what if today is Sunday. is yesterday "last" saturday?
    Depends on whether or not you are in the last week or not :

    Quote Originally Posted by zhshqzyc View Post
    Thank you very much.
    If I only want to get last Saturday based on Today, how to get it?
    Today is a variable.
    I think I should work something out, give me a moment or 2

  7. #7
    Join Date
    Jul 2001
    Location
    Sunny South Africa
    Posts
    11,283

    Thumbs up Re: Get last Saturday and last Sunday

    OK, I've got something working

    With this app, you can enter any date, as well as the day you wish ( for example, if you want the last Saturday, you would enter 7, if you want the last Sunday, you would enter 1 ). What will happen then, is, it will show you the precise date of the last Saturday or Sunday you want

    Here's what the code looks like :
    Code:
    Public Class Form1
    
        Private TestDate As Date = #5/3/2009# 'Sample Date We Can Use
    
        Private Sub GetLastDayIWant(ByVal SourceDate As Date, ByVal LastDayWanted As Integer)
    
            Dim LastDayOfSourceMonth As Integer = Date.DaysInMonth(SourceDate.Year, SourceDate.Month)
            Dim SourceWeekDay As Integer = Weekday(SourceDate)
            Dim ProducedDate As Date
            Select Case SourceWeekDay
                Case 1 ' Sunday
                    Select Case LastDayWanted
                        Case 1 'Sunday
                            ProducedDate = New Date(SourceDate.Year, SourceDate.Month, LastDayOfSourceMonth)
                        Case 2 'Monday
                            ProducedDate = New Date(SourceDate.Year, SourceDate.Month, LastDayOfSourceMonth - 6)
                        Case 3 'Tuesday
                            ProducedDate = New Date(SourceDate.Year, SourceDate.Month, LastDayOfSourceMonth - 5)
                        Case 4 'Wednesday
                            ProducedDate = New Date(SourceDate.Year, SourceDate.Month, LastDayOfSourceMonth - 4)
                        Case 5 'Thursday
                            ProducedDate = New Date(SourceDate.Year, SourceDate.Month, LastDayOfSourceMonth - 3)
                        Case 6 ' Friday
                            ProducedDate = New Date(SourceDate.Year, SourceDate.Month, LastDayOfSourceMonth - 2)
                        Case 7 'Saturday
                            ProducedDate = New Date(SourceDate.Year, SourceDate.Month, LastDayOfSourceMonth - 1)
                    End Select
                Case 2 'Monday
                    Select Case LastDayWanted
                        Case 1 'Sunday
                            ProducedDate = New Date(SourceDate.Year, SourceDate.Month, LastDayOfSourceMonth - 1)
                        Case 2 'Monday
                            ProducedDate = New Date(SourceDate.Year, SourceDate.Month, LastDayOfSourceMonth)
                        Case 3 'Tuesday
                            ProducedDate = New Date(SourceDate.Year, SourceDate.Month, LastDayOfSourceMonth - 6)
                        Case 4 'Wednesday
                            ProducedDate = New Date(SourceDate.Year, SourceDate.Month, LastDayOfSourceMonth - 5)
                        Case 5 'Thursday
                            ProducedDate = New Date(SourceDate.Year, SourceDate.Month, LastDayOfSourceMonth - 4)
                        Case 6 ' Friday
                            ProducedDate = New Date(SourceDate.Year, SourceDate.Month, LastDayOfSourceMonth - 3)
                        Case 7 'Saturday
                            ProducedDate = New Date(SourceDate.Year, SourceDate.Month, LastDayOfSourceMonth - 2)
                    End Select
                Case 3 'Tuesday
                    Select Case LastDayWanted
                        Case 1 'Sunday
                            ProducedDate = New Date(SourceDate.Year, SourceDate.Month, LastDayOfSourceMonth - 2)
                        Case 2 'Monday
                            ProducedDate = New Date(SourceDate.Year, SourceDate.Month, LastDayOfSourceMonth - 1)
                        Case 3 'Tuesday
                            ProducedDate = New Date(SourceDate.Year, SourceDate.Month, LastDayOfSourceMonth)
                        Case 4 'Wednesday
                            ProducedDate = New Date(SourceDate.Year, SourceDate.Month, LastDayOfSourceMonth - 6)
                        Case 5 'Thursday
                            ProducedDate = New Date(SourceDate.Year, SourceDate.Month, LastDayOfSourceMonth - 5)
                        Case 6 ' Friday
                            ProducedDate = New Date(SourceDate.Year, SourceDate.Month, LastDayOfSourceMonth - 4)
                        Case 7 'Saturday
                            ProducedDate = New Date(SourceDate.Year, SourceDate.Month, LastDayOfSourceMonth - 3)
                    End Select
                Case 4 'Wednesday
                    Select Case LastDayWanted
                        Case 1 'Sunday
                            ProducedDate = New Date(SourceDate.Year, SourceDate.Month, LastDayOfSourceMonth - 3)
                        Case 2 'Monday
                            ProducedDate = New Date(SourceDate.Year, SourceDate.Month, LastDayOfSourceMonth - 2)
                        Case 3 'Tuesday
                            ProducedDate = New Date(SourceDate.Year, SourceDate.Month, LastDayOfSourceMonth - 1)
                        Case 4 'Wednesday
                            ProducedDate = New Date(SourceDate.Year, SourceDate.Month, LastDayOfSourceMonth)
                        Case 5 'Thursday
                            ProducedDate = New Date(SourceDate.Year, SourceDate.Month, LastDayOfSourceMonth - 6)
                        Case 6 ' Friday
                            ProducedDate = New Date(SourceDate.Year, SourceDate.Month, LastDayOfSourceMonth - 5)
                        Case 7 'Saturday
                            ProducedDate = New Date(SourceDate.Year, SourceDate.Month, LastDayOfSourceMonth - 4)
                    End Select
                Case 5 'Thursday
                    Select Case LastDayWanted
                        Case 1 'Sunday
                            ProducedDate = New Date(SourceDate.Year, SourceDate.Month, LastDayOfSourceMonth - 4)
                        Case 2 'Monday
                            ProducedDate = New Date(SourceDate.Year, SourceDate.Month, LastDayOfSourceMonth - 3)
                        Case 3 'Tuesday
                            ProducedDate = New Date(SourceDate.Year, SourceDate.Month, LastDayOfSourceMonth - 2)
                        Case 4 'Wednesday
                            ProducedDate = New Date(SourceDate.Year, SourceDate.Month, LastDayOfSourceMonth - 1)
                        Case 5 'Thursday
                            ProducedDate = New Date(SourceDate.Year, SourceDate.Month, LastDayOfSourceMonth)
                        Case 6 ' Friday
                            ProducedDate = New Date(SourceDate.Year, SourceDate.Month, LastDayOfSourceMonth - 6)
                        Case 7 'Saturday
                            ProducedDate = New Date(SourceDate.Year, SourceDate.Month, LastDayOfSourceMonth - 5)
                    End Select
                Case 6 'Friday
                    Select Case LastDayWanted
                        Case 1 'Sunday
                            ProducedDate = New Date(SourceDate.Year, SourceDate.Month, LastDayOfSourceMonth - 5)
                        Case 2 'Monday
                            ProducedDate = New Date(SourceDate.Year, SourceDate.Month, LastDayOfSourceMonth - 4)
                        Case 3 'Tuesday
                            ProducedDate = New Date(SourceDate.Year, SourceDate.Month, LastDayOfSourceMonth - 3)
                        Case 4 'Wednesday
                            ProducedDate = New Date(SourceDate.Year, SourceDate.Month, LastDayOfSourceMonth - 2)
                        Case 5 'Thursday
                            ProducedDate = New Date(SourceDate.Year, SourceDate.Month, LastDayOfSourceMonth - 1)
                        Case 6 ' Friday
                            ProducedDate = New Date(SourceDate.Year, SourceDate.Month, LastDayOfSourceMonth)
                        Case 7 'Saturday
                            ProducedDate = New Date(SourceDate.Year, SourceDate.Month, LastDayOfSourceMonth - 6)
                    End Select
                Case 7 'Saturday
                    Select Case LastDayWanted
                        Case 1 'Sunday
                            ProducedDate = New Date(SourceDate.Year, SourceDate.Month, LastDayOfSourceMonth - 6)
                        Case 2 'Monday
                            ProducedDate = New Date(SourceDate.Year, SourceDate.Month, LastDayOfSourceMonth - 5)
                        Case 3 'Tuesday
                            ProducedDate = New Date(SourceDate.Year, SourceDate.Month, LastDayOfSourceMonth - 4)
                        Case 4 'Wednesday
                            ProducedDate = New Date(SourceDate.Year, SourceDate.Month, LastDayOfSourceMonth - 3)
                        Case 5 'Thursday
                            ProducedDate = New Date(SourceDate.Year, SourceDate.Month, LastDayOfSourceMonth - 2)
                        Case 6 ' Friday
                            ProducedDate = New Date(SourceDate.Year, SourceDate.Month, LastDayOfSourceMonth - 1)
                        Case 7 'Saturday
                            ProducedDate = New Date(SourceDate.Year, SourceDate.Month, LastDayOfSourceMonth)
                    End Select
            End Select
    
            MessageBox.Show(ProducedDate.ToString)
        End Sub
    
    
        Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
            GetLastDayIWant(TestDate, 6)
        End Sub
    End Class
    The logic I used was simple. I first determined how many days are in the desired month, then determined what day that was, based on that, I just subtracted the number of days, from the last day, to produce the result

    Yes, it may seem long, but I had to do it for every day, so it's not really that complicated
    If anyone can optimize this a bit further, feel free to share it with us


    I'm sending the project with, I sincerely hope it helps
    Last edited by HanneSThEGreaT; June 14th, 2010 at 05:39 AM.

  8. #8
    Join Date
    Jan 2006
    Location
    Fox Lake, IL
    Posts
    15,007

    Re: Get last Saturday and last Sunday

    This works as well:


    Code:
    Dim d As Date = Now
    While d.Month = Now.Month
      d = d.AddDays(-1)
    End While
    
    While d.DayOfWeek <> DayOfWeek.Sunday
      d = d.AddDays(-1)
    End While
    
    MessageBox.Show("The last sunday of last month is " & d.ToLongDateString)
    David

    CodeGuru Article: Bound Controls are Evil-VB6
    2013 Samples: MS CODE Samples

    CodeGuru Reviewer
    2006 Dell CSP
    2006, 2007 & 2008 MVP Visual Basic
    If your question has been answered satisfactorily, and it has been helpful, then, please, Rate this Post!

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured