-1

I have two datetimepickers. So I want to get the days of the week between the selected values.

Example: date1 = 14/1/2015 and date2 = 17/1/2015

So the result should be: days = Wed. Thu. Fri. Sat.

Michele Mariotti
  • 7,372
  • 5
  • 41
  • 73
OJazem
  • 89
  • 1
  • 2
  • 11
  • 2
    Can I ask what you tried? Can you please share your effort so we can see what you tried, this will help us see where things are going wrong so we can help you better. As it stands, it reads like: "do my work for me" which may not be well received. You can [edit your post](http://stackoverflow.com/posts/27938399/edit) – MyDaftQuestions Jan 14 '15 at 08:15
  • days of week == 7 days per week OR workdays (i.e. no sunday, no saturday)? – DrKoch Jan 14 '15 at 08:16
  • Have you tried anything? Your problem can be easily solved by just simple loop from date1 to date2 – Andrey Korneyev Jan 14 '15 at 08:16
  • There is an answer in stackoverflow for this :) [http://stackoverflow.com/questions/1617049/calculate-the-number-of-business-days-between-two-dates][1] [1]: http://stackoverflow.com/questions/1617049/calculate-the-number-of-business-days-between-two-dates – Danil Jan 14 '15 at 08:16
  • @MyDaftQuestions I did some coding, but all what I got is calculating the number of days only. – OJazem Jan 14 '15 at 08:30
  • @AndyKorneyev I did some coding, but all what I got is calculating the number of days only. – OJazem Jan 14 '15 at 08:30
  • @Danil The linked provided doesnt meet my request. That provides the days number between two dates. – OJazem Jan 14 '15 at 08:31
  • Where is John, offering is awesome library JodaTime? :) – DerApe Jan 14 '15 at 08:36
  • What if the date range is bigger (i.e. more than a week)? Do you want each `DayOfWeek` once, or do you want to be able to tell for example how many wednesdays are in between the two dates? – Corak Jan 14 '15 at 08:41
  • @Corak If the date is more than one week, the result will be one week and 2 days (for example) or it can be two weeks and 3 days. 2 weeks and Sat. Sun. Mon. – OJazem Jan 14 '15 at 08:48
  • @OJazem - So you want one entry for every actual day. Later, when you're working with that sequence of `DayOfWeek` items, do you want to be able to tell the actual date again? – Corak Jan 14 '15 at 09:02
  • @Corak Actually I want to get all the selected days in a variable and number of weeks in another variable. no need to tell the actual date of each day (if u mean that). the selected date is enough. – OJazem Jan 14 '15 at 09:07

3 Answers3

6

You want to get the DayOfWeeks of all DateTimes between two dates?

int daysDiff = (date2 - date1).Days;
List<DayOfWeek> days = Enumerable.Range(0, daysDiff + 1) // +1 because you want to include start and end date
    .Select(d => date1.AddDays(d).DayOfWeek)
    .ToList();

You need to add using System.Linq;

Tim Schmelter
  • 450,073
  • 74
  • 686
  • 939
5

You can easily iterate your DateTime values like;

var dt1 = new DateTime(2015, 1, 14);
var dt2 = new DateTime(2015, 1, 17);
while (dt2 >= dt1)
{
    Console.WriteLine(dt1.DayOfWeek);
    dt1 = dt1.AddDays(1);
}

Result will be;

Wednesday
Thursday
Friday
Saturday

If you wanna their abbreviated day names as Wed, Thu, Fri, Sat, you can use "ddd" custom format specifier with a english-based culture (like InvariantCulture) like;

var dt1 = new DateTime(2015, 1, 14);
var dt2 = new DateTime(2015, 1, 17);
while (dt2 >= dt1)
{
    Console.WriteLine(dt1.ToString("ddd", CultureInfo.InvariantCulture));
    dt1 = dt1.AddDays(1);
}
Soner Gönül
  • 97,193
  • 102
  • 206
  • 364
1

Another alternative, which includes non-usual use of a for-loop and the yield return statement:

void Main()
{
    var daysOfWeek = DaysBetween(
        new DateTime(2015, 1, 14), 
        new DateTime(2015, 1, 17));

    Console.WriteLine(
        String.Join(", ", daysOfWeek.Select(d => d.ToString().Substring(0, 3))));

    // prints: Wed, Thu, Fri, Sat
}

IEnumerable<DayOfWeek> DaysBetween(DateTime start, DateTime end)
{
    for (var dateTime = start; dateTime <= end; dateTime = dateTime.AddDays(1))
    {
        yield return dateTime.DayOfWeek;
    }
}
dav_i
  • 27,509
  • 17
  • 104
  • 136