How to find days in year except Saturday and Sunday in C#

added by jalpesh
11/30/2013 8:50:32 AM

2 Kicks, 119 Views

Before 2 years I have written a blog post to find Saturday and Sunday in given date range. It has gain a lot of popularity and on that blog post one user ask about how we can find days in year except Saturday and Sunday in C#. This post is a in reply for that. Following is a code for that. using System; namespace DatimeApplication { class Program { static void Main(string[] args) { DateTime startDate = new DateTime(2013, 1, 1); DateTime endDate = new DateTime(2013, 12, 31); TimeSpan diff = endDate - startDate; int days = diff.Days; int j=0; for (var i = 0; i <= days; i++) { var testDate = startDate.AddDays(i); if (testDate.DayOfWeek != DayOfWeek.Saturday && testDate.DayOfWeek != DayOfWeek.Sunday) { j = j + 1; Console.WriteLine(testDate.ToShortDateString()); } } Console.WriteLine(string.Format("Total Days:{0}",j)); Console.ReadLine(); } } }


0 comments