how to display all the mondays when you select monday in a calendar using Calendar control asp.net c#

protected void Button1_Click1(object sender, EventArgs e)
{

    DateTime dtime1 = new DateTime();
    dtime1 = Calendar1.SelectedDate;

    DateTime dtime = new DateTime();
    dtime = dtime1.AddDays(7);
    Response.Write(dtime.ToString("dd/MM/yyyy") + "</br>");
}

I need to display all the Mondays when i select Monday of the current month. Should i use bool? I have to code using loop concepts.

Answers


DateTime dtime1 = Calendar1.SelectedDate;         

int iTotalDays = DateTime.DaysInMonth(dtime1.Year, dtime1.Month) - dtime1.Day;

for (int i = 1; i <= iTotalDays; i++)
{
  var d = dtime1.AddDays(i);
  if (d.DayOfWeek == DayOfWeek.Monday) 
       Response.Write(d.ToString("dd/MM/yyyy") + "</br>");
}

UPDATE:

All mondays of current month:

DateTime dtime1 = DateTime.Now;       

int iTotalDays = DateTime.DaysInMonth(dtime1.Year, dtime1.Month);

for (int i = 1; i <= iTotalDays; i++)
{
    var d = new DateTime(dtime1.Year, dtime1.Month, i);
    if (d.DayOfWeek == DayOfWeek.Monday)          
        Response.Write(d.ToString("dd/MM/yyyy") + "</br>");
}

I think the following code should help you in displaying all the Mondays of a particular month.

protected void Button1_Click1(object sender, EventArgs e) {

    DateTime dtime1 = new DateTime();
    dtime1 = Calendar1.SelectedDate;
    // get a list of all dates
    var dates = Enumerable.Range(1, DateTime.DaysInMonth(dtime1.Year, dtime1.Month)).Select(n => new DateTime(dtime1.Year, dtime1.Month, n));
    // then filter mondays like following
    var mondays = from d in dates
                   where d.DayOfWeek == DayOfWeek.Monday
                   select d;
    List<DateTime> listOfMondays = mondays.ToList();
    // then you can loop through the listOfMondays and write it on the response.

}

Need Your Help

cakePHP paginator not passing passedargs

cakephp components paginator

I am using cakePHP and I am trying to get the paginator component to pass the get variables, or passedargs, when you click through to different pages. I have a variety of different search input sel...

About UNIX Resources Network

Original, collect and organize Developers related documents, information and materials, contains jQuery, Html, CSS, MySQL, .NET, ASP.NET, SQL, objective-c, iPhone, Ruby on Rails, C, SQL Server, Ruby, Arrays, Regex, ASP.NET MVC, WPF, XML, Ajax, DataBase, and so on.