Finding Saturday,Sunday between date range in C#/ASP.NETI know this post will sound basic to some of the people. But yesterday one of my reader asked me this question. So I decided to write this blog post . So lets first create a console application which will find the Saturday,Sunday between two dates just like following.
Once you are ready with Console application. You need to write following code in that to find and print Saturday and Sunday between particular date range.
using System;
namespace DatimeApplication
{
class Program
{
static void Main(string[] args)
{
DateTime startDate=new DateTime(2011,3,1);
DateTime endDate = DateTime.Now;
TimeSpan diff = endDate - startDate;
int days = diff.Days;
for (var i = 0; i <= days; i++)
{
var testDate = startDate.AddDays(i);
switch (testDate.DayOfWeek)
{
case DayOfWeek.Saturday:
case DayOfWeek.Sunday:
Console.WriteLine(testDate.ToShortDateString());
break;
}
}
Console.ReadLine();
}
}
}
As you can see in above code I am finding Saturday and Sunday between 1st March 2011 and today. So I have taken two variables called startDate and endDate. After that I have got difference between them and then via for loop I am checking that day of week is Saturday or Sunday then I am printing that. Now lets run the application and lets check browser and Here you can see below output as expected.
So as you can see it is very Easy. You can find any weekday that way. Hope you liked it. Stay tuned for more..