How to show all first sunday in grid from the range of two date
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.
Answers (2)
0
here is the code you wanted:
using System; using System.Collections.Generic; using System.Linq; using System.Windows.Forms;
namespace WindowsFormsApplication3 { static class Program { /// <summary> /// The main entry point for the application. /// </summary> [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(); } } }
|
Accepted 0
Hi Roei Bar,
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 { /// <summary> /// The main entry point for the application. /// </summary> [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 < span.TotalDays; 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
