-1

I have a form where the user can enter a from and to date, and when the form is submitted i will generate an excel file that contains the range of weeks in the specified from to date.

As an example if the user entered: 1/8/2018 - 15/8/2018 the excel file will contains the following columns:

-1/8/2018 to 7/8/2018

-8/8/2018 to 14/8/2018

-15/8/2018 to 21/8/2018

how can i achieve to split the date range by weeks

Appreciate any help.

Sora
  • 2,465
  • 18
  • 73
  • 146
  • 4
    What have you tried so far? – Tim Schmelter Aug 01 '18 at 08:43
  • 1
    So your weeks last 8 days? – Rafalon Aug 01 '18 at 08:44
  • @TimSchmelter i tired to use the example of this link: https://stackoverflow.com/questions/7363978/get-a-list-of-weeks-for-a-year-with-dates but i didn't manage to get the weeks between two date as this example is only for a year – Sora Aug 01 '18 at 08:46
  • @Rafalon it is 7 days from 1/1 to 8/1 count them you will see it's 7 not 8 – Sora Aug 01 '18 at 08:47
  • 2
    @Sora 1, 2, 3, 4, 5, 6, 7, 8 - oh 8? Think of it this way: the first day of your first range is a wednesday, the first day of your second range is a... thursday? – Rafalon Aug 01 '18 at 08:47
  • @Sora it's only 7 days if you exclude the first day. The first day (today) is a Wednesday - Wednesday only appears once in a week, so you either need to count today (1/8), or next week's Wednesday (8/8), not both. – Diado Aug 01 '18 at 08:49
  • The first day of all of my weeks is Monday, why would the first day of your weeks change every week? – Rafalon Aug 01 '18 at 08:50
  • I've updated my question – Sora Aug 01 '18 at 08:50
  • 1
    Good, now what happened to `15/08/2018`? I mean what do you want to happen if user does not input full week range – Rafalon Aug 01 '18 at 08:51
  • @Rafalon the user have the ability choose from the calendar input any range of date and i need to calculate the weeks from the range he chosen – Sora Aug 01 '18 at 08:51
  • If I give you 1. of August 2018, to 7th of August 2018, what exact numbers do you want to return? – Lasse V. Karlsen Aug 01 '18 at 08:52
  • @Rafalon check my updated question, there will be an additional 1 week to be added – Sora Aug 01 '18 at 08:53
  • @LasseVågsætherKarlsen the excel will contains 1 column wish is `1/8/2018 to 7/8/2018` – Sora Aug 01 '18 at 08:54
  • OK, so you want to split the dates by week separator, not "get week range" (subject)? I misunderstood your question then. – Lasse V. Karlsen Aug 01 '18 at 10:49

2 Answers2

7

My idea is: You cumulate a chunkDay to the start date (in your case, from 1 to 7 is 6 days) until the date end. The fiddle works: https://dotnetfiddle.net/rrN8Yd

It returns the last week as your requirement 15/8/2018 to 21/8/2018

    public static IEnumerable<Tuple<DateTime, DateTime>> Split(DateTime start, DateTime end, int chunk)
    {
        DateTime chunkEnd;
        while ((chunkEnd = start.AddDays(chunk)) < end)
        {
            yield return Tuple.Create(start, chunkEnd);
            start = chunkEnd.AddDays(1);
        }
        yield return Tuple.Create(start, start.AddDays(chunk));
    }

With this input

    foreach( var t in Split(new DateTime(2018,8,1), new DateTime(2018,8,17),6)){
            Console.WriteLine(t.Item1.ToString() + " " + t.Item2.ToString());
        }

I have the output

8/1/2018 12:00:00 AM 8/7/2018 12:00:00 AM
8/8/2018 12:00:00 AM 8/14/2018 12:00:00 AM
8/15/2018 12:00:00 AM 8/21/2018 12:00:00 AM
Antoine V
  • 6,998
  • 2
  • 11
  • 34
0
using System.IO;
using System;

class Program
{
    static void Main()
    {
        DateTime fromDate = new DateTime(2018,8,1);
        DateTime toDate = new DateTime(2018,8,15);
        Console.WriteLine("From: "+fromDate.ToString("dd/MM/yyyy"));
        Console.WriteLine("To  : "+toDate.ToString("dd/MM/yyyy"));

        DateTime startDate = fromDate;

        do
        {
            Console.WriteLine(startDate.ToString("dd/MM/yyyy")+"-"+startDate.AddDays(6).ToString("dd/MM/yyyy"));
            startDate = startDate.AddDays(7);
        }
        while(startDate < toDate.AddDays(1));
    }
}

Try it online

(do{}while() or while(){} would be the same)

Rafalon
  • 4,450
  • 2
  • 16
  • 30