locked
Getting Week Start Date and End Date from a Date. Problem: For 05/01/2013 date, our function should return StartDate=04/29/2013(Mon) and EndDate=05/03/2013(Fri) RRS feed

  • Question

  • User1558924997 posted

    Below code fails for 05/01/2013 by returning  05/-1/2013, we can check this condition and resolve the issue but still looking for expert observations on any Generic Solution.

    dim WeekStart = Day("05/01/2013") - (CType("05/01/2013", Date).DayOfWeek - 1)
    
    dim startDate = String.Format("{0}/{1}/{2}", _
    Month("05/01/2013"), _
    Day("05/01/2013") - (CType("05/01/2013", Date).DayOfWeek - 1), _
    Year("05/01/2013"))
    endDate = startDate.AddDays(4)



    Wednesday, May 1, 2013 4:53 AM

Answers

  • User1508394307 posted

    The first code in original post is somehow strange because it takes Day("05/01/2013") which is 5 and then use .DayOfWeek which is also 5 and has different meaning. 

    To find start date of week, you can simply do

    Dim d As Date = #5/1/2013#   <--- some date
    Dim s As Date
    While d.DayOfWeek <> DayOfWeek.Monday  <---- search until date is not Monday (if your week starts with Sunday, change it)
       d = d.AddDays(-1)
    End While
    s = d

    Hope this helps.

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Wednesday, May 1, 2013 5:24 AM

All replies

  • User1558924997 posted

    Updated Solution 

    dim startDate = CType("05/01/2013", Date).AddDays(CType("05/01/2013", Date).DayOfWeek - DayOfWeek.Friday)
    dim endDate = startDate.AddDays(4)

    Works for all cases.

    Wednesday, May 1, 2013 5:12 AM
  • User-1716253493 posted

    Sorry, i am not sure understand what you mean with your code

    I just want to suggest you, if you want to calculate datetime value better using datetime datatype.

    Dont convert it to string or format it then manipulate the string, you can format it if you already get the final value

    Dim startDate As DateTime = DateTime.Parse("05/01/2013")
    Dim WeekStart As Integer = startDate.DayOfWeek
    Dim endDate As DateTime = startDate.AddDays(4)



    Wednesday, May 1, 2013 5:17 AM
  • User1558924997 posted

    Hi,

    Actual Code is below. Date is not hard Coded, its a Variable of Date Type.

    startDate = startDate.AddDays(startDate.DayOfWeek - DayOfWeek.Friday)
    endDate = startDate.AddDays(4)

    We have use Date Literal Just for explaining :)

    Wednesday, May 1, 2013 5:20 AM
  • User1508394307 posted

    The first code in original post is somehow strange because it takes Day("05/01/2013") which is 5 and then use .DayOfWeek which is also 5 and has different meaning. 

    To find start date of week, you can simply do

    Dim d As Date = #5/1/2013#   <--- some date
    Dim s As Date
    While d.DayOfWeek <> DayOfWeek.Monday  <---- search until date is not Monday (if your week starts with Sunday, change it)
       d = d.AddDays(-1)
    End While
    s = d

    Hope this helps.

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Wednesday, May 1, 2013 5:24 AM
  • User1558924997 posted

    Hi

    smirnov

     Day("05/01/2013")= 1, as its in MM/dd/yyyy format.

    Wednesday, May 1, 2013 5:31 AM