Click here to Skip to main content
16,016,744 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi!
I have a dropdownlist with days of the week. Now I have some kind of a pseudo code of how to write the application.

What I want is when the day in my combobox is selected, I should get the date of that day for the current week.

something like this;

C#
currentday==DateTime.Now;
date=""

if(Dropdwonlist="tuesday")
{
 currentday+1
}
if(Dropdwonlist="wednesday")
{
 currentday+2
}


What I want here is to get this week's Tuesday/Wednesday date.
The values "1 and 2" represent the day.

Thanks for your time!
Posted
Updated 13-Sep-11 4:28am
v2

First you'll have to find out the start date of the current week. Please take a former solution I posted here on CP as a reference on how to do that: I want to get all the list of week[^].

It's a piece of cake from there. All you have to do then as add the appropriate number of days to that start date according to your combo list box. You should remember though that different cultures have differen starting days for a week. Please also refer to my former solution for that.

Cheers!

—MRB
 
Share this answer
 
Here a solution based on the Week class of the Time Period Library for .NET[^]:
C#
// ----------------------------------------------------------------------
public DateTime GetDayOfCurrentWeek( DayOfWeek dayOfWeek )
{
  Week week = new Week();
  foreach ( Day day in week.GetDays() )
  {
    if ( day.DayOfWeek == dayOfWeek )
    {
      return day.Start.Date;
    }
  }
  throw new InvalidOperationException();
} // GetDayOfCurrentWeek
Please note that the Week class supports the CalendarWeekRule[^] of the current culture/calendar.
 
Share this answer
 
Comments
Anele Ngqandu 14-Sep-11 7:22am    
If i may ask should i use all the classes because week.cs inherits from another class and this other class inherits from CalendarWeekRule class. So Shud I take all these classes for now so i can see the output of your code?
Jani Giannoudis 14-Sep-11 14:37pm    
Copy the library 'Pub\Desktop.Release\Itenso.TimePeriod.dll' to you your project and add the reference to your project. After adding the using 'Itenso.TimePeriod' to your source code, you can use my sample code.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900