-1

Need help how to get first date and last date of a week in current month.

Below what I have done so far, but still fall for certain case especially where the start date and end date is sunday:

    var givenDate = new DateTime(2018,4,1);
    var intervalToStart = givenDate.DayOfWeek - DayOfWeek.Monday;
    var startDate = givenDate.AddDays(-intervalToStart);

    DateTime endDate;
    int intervalToEnd = 0; 

    if (startDate.Month != givenDate.Month)
        startDate = new DateTime(givenDate.Year, givenDate.Month, 1);

    var dayOfWeekStartDate = startDate.DayOfWeek.ToString().ToLower();

    switch (dayOfWeekStartDate)
        {
            case "sunday": intervalToEnd = 0;
                break;
            case "monday": intervalToEnd = 6;
                break;
            case "tuesday": intervalToEnd = 5;
                break;
            case "wednesday": intervalToEnd = 4;
                break;
            case "thursday": intervalToEnd = 3;
                break;
            case "friday": intervalToEnd = 2;
                break;
            case "saturday": intervalToEnd = 1;
                break;
        }

    endDate = startDate.AddDays(intervalToEnd);


    Console.WriteLine(startDate);
    Console.WriteLine(endDate);

Case 1:

  • Given date: 05 May 2018 (saturday)

  • First date of week: 01 May 2018 (tuesday)

  • Last date of week: 06 May 208 (sunday)

Case 2:

  • Given date: 09 May 2018 (wednesday)

  • First date of week: 07 May 2018 (monday)

  • Last date of week: 13 May 2018 (sunday)

Case 3:

  • Given date: 01 Apr 2018 (sunday)

  • First date of week: 01 Apr 2018 (sunday)

  • Last date of week: 01 Apr 2018 (sunday)

case 4:

  • Given date: 02 June 2018 (saturday)

  • First date of week: 01 June 2018 (friday)

  • Last date of week: 03 June 2018 (sunday)

Concept or answer is highly appreciated.

Thank you in advance.

Akbar Kautsar
  • 416
  • 5
  • 13
  • 1
    Is there a question in there somewhere? Please [edit] according to the rules specified in [ask]. Also, note that week start is culture specific - it might start on sunday, monday, saturday etc'. – Zohar Peled Apr 24 '18 at 11:31
  • 2
    Hope that this is not your first post and you are aware about [how to ask](https://stackoverflow.com/help/how-to-ask). then why are you come up with a home work? – sujith karivelil Apr 24 '18 at 11:32
  • 3
    My assumption is: Week starts on a monday, Get the date of monday preceding the given date. If the date goes to the previous month then get the first date of the month. Is that correct ? – Jones Joseph Apr 24 '18 at 11:32
  • 2
    [DateTime.DayOfWeek](https://msdn.microsoft.com/library/system.datetime.dayofweek(v=vs.110).aspx) may be a good way to start – Cid Apr 24 '18 at 11:33
  • 1
    Do your homework yourself. Programmation is all about self-teaching, learning by practice and experiencing. – N.K Apr 24 '18 at 11:34
  • 1
    Maybe **[this](https://stackoverflow.com/questions/38039/how-can-i-get-the-datetime-for-the-start-of-the-week?utm_medium=organic&utm_source=google_rich_qa&utm_campaign=google_rich_qa)** will help – Izzy Apr 24 '18 at 11:34
  • 1
    What if the given date falls into another month than the end of the week, like 31Mar 2018? The week starts at 26 Mar 2018 and ends with 01 Apr 2018 – Tim Schmelter Apr 24 '18 at 11:44
  • @TimSchmelter, yes sorry for not mention the case in the question. – Akbar Kautsar Apr 24 '18 at 11:59
  • @ZoharPeled, sorry for not to make clear question, also I have edited my question. – Akbar Kautsar Apr 24 '18 at 12:27

2 Answers2

5

You want to return the first day of the month if the monday would fall into the previous month?

public static Tuple<DateTime, DateTime> GetFirstAndLastWeekDate(DateTime dt, DayOfWeek firstDayOfWeek = DayOfWeek.Monday)
{
    int diff = (7 + (dt.DayOfWeek - firstDayOfWeek)) % 7;
    DateTime firstDay = dt.AddDays(-1 * diff).Date;
    DateTime lastDay = firstDay.AddDays(6);
    if (dt.Month != firstDay.Month)
    {
        firstDay = new DateTime(dt.Year, dt.Month, 1);
    }

    return Tuple.Create(firstDay, lastDay);
}
Tim Schmelter
  • 450,073
  • 74
  • 686
  • 939
0

Use DateTime.DayOfWeek

and the code would be like this

var givenDate = new DateTime(2018, 4, 1);
int addMax = DateTime.DaysInMonth(givenDate.Year, givenDate.Month) - givenDate.Day;
int add = Math.Min(addMax, ((DayOfWeek.Saturday - givenDate.DayOfWeek) + 1) % 7);

int subMax = givenDate.Day - 1;
int sub = Math.Min(subMax, (givenDate.DayOfWeek - DayOfWeek.Monday + 7) % 7);

var firstDate = givenDate.AddDays(-sub);
var lastDate = givenDate.AddDays(add);
Console.Write("Give: {0}({1})\nFirst: {2}({3})\nLast: {4}({5})", 
    givenDate, givenDate.DayOfWeek, 
    firstDate, firstDate.DayOfWeek, 
    lastDate, lastDate.DayOfWeek);
PARK
  • 21
  • 3