none
Get Monday of previous FULL week RRS feed

  • Question

  • Hi

    Can anyone help with some code or a function to get the date of a specified day (monday in this case) of the previous FULL week

    For example today (as I type) in UK is Sunday 3 March, so I want to provide today's date and get the date of the last Monday which of a full week, so it wouldn't be 25 February as not a full week yet - it would return 18 February

    If I run it tomorrow (Monday 4 March) then it would return 25 February

    Hope that makes sense

    P.S. Probably be useful if a function that you choose day of week - I am interested in Monday for now, but function could be used for other things if you had to provide day

    e.g. Dim date = GetPreviousDay(datetime.today, Monday)


    Darren Rose

    Sunday, March 3, 2019 11:06 PM

Answers

  • Hello,

    Okay here is a full edit. The code sample below utilizes Noda Time

    NuGet home for Noda Time.

    Noda Time home page.

    Button click event

    Imports NodaTime
    Imports NodaTime.Calendars
    Public Class Form1
        Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
            For index As Integer = 2 To 12
                Dim theDate As New LocalDate(Now.Year, Now.Month, index)
                Dim rule As IWeekYearRule = WeekYearRules.Iso
    
                Dim ld As LocalDate = LocalDate.FromWeekYearWeekAndDay(
                    Now.Year, rule.GetWeekOfWeekYear(theDate) - 1, IsoDayOfWeek.Monday)
    
                Console.WriteLine($"{theDate.ToDateTimeUnspecified().
                                     ToString("MM/dd/yyyy")} -> {ld.ToString()}")
            Next
    
        End Sub
    End Class
    

    Output 

    03/02/2019 -> Monday, February 18, 2019
    03/03/2019 -> Monday, February 18, 2019
    03/04/2019 -> Monday, February 25, 2019
    03/05/2019 -> Monday, February 25, 2019
    03/06/2019 -> Monday, February 25, 2019
    03/07/2019 -> Monday, February 25, 2019
    03/08/2019 -> Monday, February 25, 2019
    03/09/2019 -> Monday, February 25, 2019
    03/10/2019 -> Monday, February 25, 2019
    03/11/2019 -> Monday, March 04, 2019
    03/12/2019 -> Monday, March 04, 2019
    



    Please remember to mark the replies as answers if they help and unmarked them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.

    NuGet BaseConnectionLibrary for database connections.

    StackOverFlow
    profile for Karen Payne on Stack Exchange



    Sunday, March 3, 2019 11:51 PM
    Moderator
  • Thanks Karen, that is very interesting - never come across Noda before

    Was hoping for a native method without using 3rd party but will have a go

    Thanks


    Darren Rose

    Hi

    Here is my attempt at doing it using native code. Not extensively tested but seems to fit the requirements.

    ' Form1 with BLANK DataGridView1 (DGV)
    ' Button1 and Label1 & Label2
    Option Strict On
    Option Explicit On
    Public Class Form1
      ' set as appropriate
      Dim ReqDOW As DayOfWeek = DayOfWeek.Monday
      Dim FirstDOW As FirstDayOfWeek = FirstDayOfWeek.Wednesday
      Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        With DGV
          .Columns.Add("Column1", "Input Date")
          .Columns.Add("Column2", "Calc Date")
          .Columns("Column1").DefaultCellStyle.Format = "ddd, dd MMM yyyy"
          .Columns("Column2").DefaultCellStyle.Format = "ddd, dd MMM yyyy"
        End With
      End Sub
      Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
        ' display only for testing
        DGV.Rows.Clear()
        ' a range about Today
        For i As Integer = -8 To 8
          DGV.Rows.Add(Now.AddDays(-i), GetWeek(Now.AddDays(-i), ReqDOW))
        Next
        ' show set DayOfWeek and
        ' FirstDayOfWeek
        Label1.Text = "FirstDayOfWeek: " & FirstDOW.ToString("F")
        Label2.Text = "ReqDayOfWeek: " & ReqDOW.ToString("F")
      End Sub
      Function GetWeek(d As Date, dow As DayOfWeek) As DateTime
        Return d.AddDays(-DatePart("w", d, FirstDOW) - (7 - dow))
      End Function
    End Class


    Regards Les, Livingston, Scotland


    • Edited by leshay Monday, March 4, 2019 5:12 PM Erroneous description corrected
    • Marked as answer by wingers Monday, March 4, 2019 5:38 PM
    Monday, March 4, 2019 4:52 PM
  • Does this work?

        Private Sub GetStartFullWeek_Click(sender As Object, e As EventArgs) Handles GetStartFullWeek.Click
            Dim Startdate As Date = DateTimePicker1.Value
            Dim DayCount As Integer = 0
            Do Until Startdate.ToString("dddd") = "Monday" And DayCount >= 7
                Startdate = Startdate.AddDays(-1)
                DayCount += 1
            Loop
            MsgBox(Startdate)
        End Sub


    Live as if you were going to die today, learn as if you were going to live forever -Mahatma Gandhi

    • Marked as answer by wingers Monday, March 4, 2019 5:38 PM
    Monday, March 4, 2019 5:28 PM

All replies

  • Hello,

    Okay here is a full edit. The code sample below utilizes Noda Time

    NuGet home for Noda Time.

    Noda Time home page.

    Button click event

    Imports NodaTime
    Imports NodaTime.Calendars
    Public Class Form1
        Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
            For index As Integer = 2 To 12
                Dim theDate As New LocalDate(Now.Year, Now.Month, index)
                Dim rule As IWeekYearRule = WeekYearRules.Iso
    
                Dim ld As LocalDate = LocalDate.FromWeekYearWeekAndDay(
                    Now.Year, rule.GetWeekOfWeekYear(theDate) - 1, IsoDayOfWeek.Monday)
    
                Console.WriteLine($"{theDate.ToDateTimeUnspecified().
                                     ToString("MM/dd/yyyy")} -> {ld.ToString()}")
            Next
    
        End Sub
    End Class
    

    Output 

    03/02/2019 -> Monday, February 18, 2019
    03/03/2019 -> Monday, February 18, 2019
    03/04/2019 -> Monday, February 25, 2019
    03/05/2019 -> Monday, February 25, 2019
    03/06/2019 -> Monday, February 25, 2019
    03/07/2019 -> Monday, February 25, 2019
    03/08/2019 -> Monday, February 25, 2019
    03/09/2019 -> Monday, February 25, 2019
    03/10/2019 -> Monday, February 25, 2019
    03/11/2019 -> Monday, March 04, 2019
    03/12/2019 -> Monday, March 04, 2019
    



    Please remember to mark the replies as answers if they help and unmarked them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.

    NuGet BaseConnectionLibrary for database connections.

    StackOverFlow
    profile for Karen Payne on Stack Exchange



    Sunday, March 3, 2019 11:51 PM
    Moderator
  • Hi Karen

    Thanks for reply

    Yes that seems to get current week, rather than full week before

    So running using sunday 03/03 it gives me 25/02 which is not a FULL week ago as week not over until 04/03

    So running on Sunday 03/03 should give me 18/02

    Running the following day Monday 04/03 should give me 25/02

    Another example if I run it on 05/03 a Tuesday then it gives me 04/03 which is not a FULL week ago

    This seems to be same issue I had with all other solutions I found


    Darren Rose

    Monday, March 4, 2019 12:38 AM
  • Hi Darren,

    I just rewrote my reply, check it out.


    Please remember to mark the replies as answers if they help and unmarked them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.

    NuGet BaseConnectionLibrary for database connections.

    StackOverFlow
    profile for Karen Payne on Stack Exchange

    Monday, March 4, 2019 12:43 AM
    Moderator
  • Thanks Karen, that is very interesting - never come across Noda before

    Was hoping for a native method without using 3rd party but will have a go

    Thanks


    Darren Rose

    Monday, March 4, 2019 3:30 PM
  • Thanks Karen, that is very interesting - never come across Noda before

    Was hoping for a native method without using 3rd party but will have a go

    Thanks


    Darren Rose

    There are many benefits to using NodaTime which may not be apparent at first like being able to run scenarios against date and times without worrying about the current computer date-time settings and much more. 

    Please remember to mark the replies as answers if they help and unmarked them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.

    NuGet BaseConnectionLibrary for database connections.

    StackOverFlow
    profile for Karen Payne on Stack Exchange

    Monday, March 4, 2019 3:49 PM
    Moderator
  • Thanks Karen, that is very interesting - never come across Noda before

    Was hoping for a native method without using 3rd party but will have a go

    Thanks


    Darren Rose

    Hi

    Here is my attempt at doing it using native code. Not extensively tested but seems to fit the requirements.

    ' Form1 with BLANK DataGridView1 (DGV)
    ' Button1 and Label1 & Label2
    Option Strict On
    Option Explicit On
    Public Class Form1
      ' set as appropriate
      Dim ReqDOW As DayOfWeek = DayOfWeek.Monday
      Dim FirstDOW As FirstDayOfWeek = FirstDayOfWeek.Wednesday
      Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        With DGV
          .Columns.Add("Column1", "Input Date")
          .Columns.Add("Column2", "Calc Date")
          .Columns("Column1").DefaultCellStyle.Format = "ddd, dd MMM yyyy"
          .Columns("Column2").DefaultCellStyle.Format = "ddd, dd MMM yyyy"
        End With
      End Sub
      Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
        ' display only for testing
        DGV.Rows.Clear()
        ' a range about Today
        For i As Integer = -8 To 8
          DGV.Rows.Add(Now.AddDays(-i), GetWeek(Now.AddDays(-i), ReqDOW))
        Next
        ' show set DayOfWeek and
        ' FirstDayOfWeek
        Label1.Text = "FirstDayOfWeek: " & FirstDOW.ToString("F")
        Label2.Text = "ReqDayOfWeek: " & ReqDOW.ToString("F")
      End Sub
      Function GetWeek(d As Date, dow As DayOfWeek) As DateTime
        Return d.AddDays(-DatePart("w", d, FirstDOW) - (7 - dow))
      End Function
    End Class


    Regards Les, Livingston, Scotland


    • Edited by leshay Monday, March 4, 2019 5:12 PM Erroneous description corrected
    • Marked as answer by wingers Monday, March 4, 2019 5:38 PM
    Monday, March 4, 2019 4:52 PM
  • Does this work?

        Private Sub GetStartFullWeek_Click(sender As Object, e As EventArgs) Handles GetStartFullWeek.Click
            Dim Startdate As Date = DateTimePicker1.Value
            Dim DayCount As Integer = 0
            Do Until Startdate.ToString("dddd") = "Monday" And DayCount >= 7
                Startdate = Startdate.AddDays(-1)
                DayCount += 1
            Loop
            MsgBox(Startdate)
        End Sub


    Live as if you were going to die today, learn as if you were going to live forever -Mahatma Gandhi

    • Marked as answer by wingers Monday, March 4, 2019 5:38 PM
    Monday, March 4, 2019 5:28 PM
  • Does this work?

        Private Sub GetStartFullWeek_Click(sender As Object, e As EventArgs) Handles GetStartFullWeek.Click
            Dim Startdate As Date = DateTimePicker1.Value
            Dim DayCount As Integer = 0
            Do Until Startdate.ToString("dddd") = "Monday" And DayCount >= 7
                Startdate = Startdate.AddDays(-1)
                DayCount += 1
            Loop
            MsgBox(Startdate)
        End Sub


    Live as if you were going to die today, learn as if you were going to live forever -Mahatma Gandhi

    Hi Gtripodi

    Yes that does seem to work - thanks

    Converted it to a function so I can call it easily and get result returned

        Private Function GetStartFullWeek(ByVal startdate As DateTime) As DateTime
            Dim DayCount As Integer = 0
            Do Until Startdate.ToString("dddd") = "Monday" And DayCount >= 7
                Startdate = Startdate.AddDays(-1)
                DayCount += 1
            Loop
            Return startdate
        End Function


    Darren Rose

    Monday, March 4, 2019 5:33 PM
  • Hi

    Here is my attempt at doing it using native code. Not extensively tested but seems to fit the requirements.

    ...

    Hi Les

    Thanks - that seems to work as well :)


    Darren Rose

    Monday, March 4, 2019 5:38 PM