3

I have a web application where the user will input 2 dates. A StartDate and an EndDate. Now I want to write it so that when StartDate and EndDate is selected to determine how many weeks there are and then to display the dates of those weeks for example if the user selects 01-11-2018 as the StartDate and 31-12-2018 as the EndDate then I want to display the following and keep in mind the Weeks are just for reference as to how it is going to look:

  1. Week 98 : 01 November 2018 - 03 November 2018
  2. Week 99 : 04 November 2018 - 10 November 2018
  3. Week 100 : 11 November 2018 - 17 November 2018

I already have the amount of weeks by using this previous post.

Now I just want to be able to to display each individual weeks Start and End date in all the weeks. I tried creating a list with the weeks and then using a Foreach to check the weeks added but this is not quite right. I am just searching for the most efficient way of accomplishing this goal.

Links also checked with similar problems are : Link1

Ruben Redman
  • 489
  • 1
  • 6
  • 21
  • How did you get "Week **98**"? – Rafalon Jan 16 '19 at 09:53
  • Do you want the week dates according to .NET or according to ISO 8601? How in the world is 1 November 2018 -> 3 November 2018 a week, and the week 98? – Jcl Jan 16 '19 at 09:55
  • The number was just an example of how it currently looks but I used the following code for that: Date = StartDate.ToString("yyyy ") + "(Week " + DateTimeFormatInfo.CurrentInfo.Calendar.GetWeekOfYear(StartDate, CalendarWeekRule.FirstDay, DayOfWeek.Monday) + ") – Ruben Redman Jan 16 '19 at 09:56
  • @Jcl I just used the week number as an example and in our company we work from the 1st to the first sunday as week 1. – Ruben Redman Jan 16 '19 at 09:58
  • @RubenRedman My question was more about the fact that any week number greater than 53 feels **very strange** – Rafalon Jan 16 '19 at 10:06
  • Take a look here, maybe it will fit you https://stackoverflow.com/questions/662379/calculate-date-from-week-number – Artyom Jan 16 '19 at 10:11
  • Thanks @Artyom will check it out now and let you know. – Ruben Redman Jan 16 '19 at 10:15

1 Answers1

4

I have made this snippet... not sure if everything is up to spec:

var startDate = new DateTime(2018, 11, 1);
var endDate = new DateTime(2018, 12, 31);

int diff = (7 + (startDate.DayOfWeek - DayOfWeek.Monday)) % 7;
var weekStartDate = startDate.AddDays(-1 * diff).Date;
var i = 1;
var weekEndDate = DateTime.MinValue;    
while(weekEndDate < endDate) {
    weekEndDate = weekStartDate.AddDays(6);
    var shownStartDate = weekStartDate < startDate ? startDate : weekStartDate;
    var shownEndDate = weekEndDate > endDate ? endDate : weekEndDate;
    Console.WriteLine($"Week {i++}: {shownStartDate:dd MMMM yyyy} - {shownEndDate:dd MMMM yyyy}");
    weekStartDate = weekStartDate.AddDays(7);
}

This assumes your weeks are "counting", starting on the week the start date is in, and uses monday as the first day of week and sunday as the last one (the ranges you see are monday - sunday except for the first/last week, which would use the start/end date instead if it doesn't happen to be monday or sunday)

You can run it online here: https://dotnetfiddle.net/jJ4Ydu

If you also need to know which week of the year it is, then it depends if you want the .NET style or the ISO8601 style... the typical one is the latter, and you can use, for example, the method found on this answer, so it'd look something like: https://dotnetfiddle.net/oJscjF

Notice how Dec-31st-2018 (which is monday) is the week 1 of 2019 on ISO8601, but week 53 for .NET

Jcl
  • 27,696
  • 5
  • 61
  • 92
  • This worked great! Thank you very much. This is much more efficient than the loop that I had. – Ruben Redman Jan 16 '19 at 10:27
  • I'll take notice of the .NET style and ISO8601 style and just check which one they would prefer. Thanks. – Ruben Redman Jan 16 '19 at 10:28
  • Won't this code display "*1 2 3 4...*" disregarding the week number? – Rafalon Jan 16 '19 at 10:36
  • @Rafalon unless you read the bottom notes, yes... and I did that on purpose since the OP showed "week 98" on his output, so I thought it would not be year weeks (nevertheless, I included year weeks on the bottom for completion, and in two variants) – Jcl Jan 16 '19 at 11:00
  • @Jcl Right, sorry I didn't see this note. Nice explanation :) – Rafalon Jan 16 '19 at 11:07
  • @Jcl your snippet does not work as expected, for example, on `startDate = 30 Dec 2018` `endDate = 20 Jan 2019` – Woldemar89 Jan 16 '19 at 11:31
  • @VladimirP. That's right, the `<=` should be `<`, I'll correct – Jcl Jan 16 '19 at 11:33