Hello friends,
I am a new member in this corner. So I hopes you will help me and try to give me solution of my question.
In my web form , I have two asp.net calendar control. Based on two selected date like first is 17/10/2009 and second one is 17/10/2010 then need to show all first sunday between this two date range in gridview.
Output is like:
1/11/2009
6/12/2009
3/01/2010
7/02/2010 and so on...
Note:- Date format here is dd/mm/yyyy
Please reply me if anyone have solution or idea.
Thanks in advance.
Loading

Roei BarRank #1,271 · 1.4K reputationPosted Sep 17, 2009, 11:32 AM EDT
using System; using System.Collections.Generic; using System.Linq; using System.Windows.Forms; namespace WindowsFormsApplication3 { static class Program { /// /// The main entry point for the application. /// [STAThread] static void Main() { DateTime dt1 = new DateTime(2009, 10, 17); DateTime dt2 = new DateTime(2010, 10, 17); TimeSpan span = dt2 - dt1; Console.WriteLine ("There're {0} days between {1} and {2}", span.TotalDays, dt1.ToString(), dt2.ToString()); for (int i = 0; i < span.TotalDays; i++) { if (dt1.DayOfWeek == DayOfWeek.Sunday) { Console.WriteLine(dt1.Date.ToString("dd/MM/yyyy")); } dt1 = dt1.AddDays(1); } Console.ReadLine(); } } }Kinjal Shah6 reputationPosted Sep 20, 2009, 10:57 PM EDT
First of all thank you for reply me. I think your code will help me for solve my problem.
I have just do some modification in your code and its working great.
using System; using System.Collections.Generic; using System.Linq; using System.Windows.Forms; namespace WindowsFormsApplication3 { static class Program { /// /// The main entry point for the application. /// [STAThread] static void Main() { DateTime dt1 = new DateTime(2009, 10, 17); DateTime dt2 = new DateTime(2010, 10, 17); TimeSpan span = dt2 - dt1; Console.WriteLine ("There're {0} days between {1} and {2}", span.TotalDays, dt1.ToString(), dt2.ToString()); int day = 0; for (int i = 0; i if (dt1.Day % 7 != 0) { day = ((int)(dt1.Day / 7))+1; } else { day = ((int)(dt1.Day / 7)); } if (dt1.DayOfWeek == DayOfWeek.Sunday && day==1) { Console.WriteLine(dt1.Date.ToString("dd/MM/yyyy")); } dt1 = dt1.AddDays(1); } Console.ReadLine(); } } }FYI:- Marks as bold in statement which placed by me.
once again thank you