views:

56

answers:

2

Hello,

I want to use a asp.net drop down to present the user a delivery date on checkout. What I'm not sure about is how to get the specific dates. What the user should see and be able to select in the drop down is the next Monday and Tuesday for the next two weeks. Any help would be appreciated.

thanks.

+1  A: 

The simplest solution is to use the DateTime.DayOfWeek property (http://msdn.microsoft.com/en-us/library/system.datetime.dayofweek.aspx).

You start with getting today's date, or, if today is Monday and you can't deliver for two days, so the next delivery will be a week Monday, then start with tomorrow. I am not certain how you would handle if I order tomorrow could I get a delivery date for the next day, so I would need that clarified.

Get the day of the week, starting with either today or tomorrow, extracting it from a specific date as explained here: http://msdn.microsoft.com/en-us/library/system.datetime.dayofweek.aspx

If it isn't Monday or Tues then just determine how many days you need to reach Monday or Tuesday, then add that number of days and get that date, and then just add seven and get the date.

I would prefer to let .NET determine the date of seven days from now, as you may change month or years.

That is the basic approach. If you get stuck when trying to implement it, I would suggest some code so we can help you determine where you got stuck.

There are other approaches, but this is probably the simplest to understand and implement.

James Black
A: 

Here's a pretty simple suggestion using a lot of LINQ:

private void LoadDeliveryDays(int period)
{
  DateTime[] days = Enumerable.Range(1, period).Select(i => DateTime.Today.AddDays(i)).ToArray();
  DropDownList1.DataSource = (from d in days where d.DayOfWeek == DayOfWeek.Monday | d.DayOfWeek == DayOfWeek.Tuesday select d.ToString("dddd dd-MM-yyyy")).ToArray();
  DropDownList1.DataBind();
}

You probably want to change the d.ToString("dddd dd-MM-yyyy") and/or what value is actually used in the dropdown.

Jakob Gade