Obtener la fecha reservable del día de la semana

I'm making a page to allow lecture create consultation slot, the way I make is populate button control in loop for 5 times(5 working days). I need to disabled the button got class in timetable and identify the button with button content then store into database. when lecturer click the button it pop up with date of column(day) + time slot.

Data for verify and disable button from table of timetable 
    start_time -> 2011-11-08 08:00:00
    end_time -> 2011-11-08 10:00:00

Then I plan to use end_time - start_time = 2 hour, if 2 hour then 08:00, 08:30, 09:00, 09:30 4 button from start_time will be disable. I create the button with

        string[] timeslot = {"08:00 AM", "08:30 AM", "09:00 AM", "09:30 AM", "10:00 AM", "10:30 AM", "11:00 AM", "11:30 AM", "12:00 PM", "12:30 PM", "01:00 PM", "01:30 PM", "02:00 PM", "02:30 PM", "03:00 PM", "03:30 PM", "04:00 PM", "04:30 PM", "05:00 PM", "05:30 PM"};
        for (int i = 0; i < 20; i++)
        {
            Button btnMonday = new Button
                                   {
                                       Height = 38,
                                       Width = 256,
                                       Content = timeslot[i],
                                       Background = new SolidColorBrush(Color.FromArgb(0xFF, 0xB1, 0xB1, 0xB1))
                                   };
            // Sets dependency properties
            btnMonday.SetValue(Grid.ColumnProperty, 0);
            btnMonday.SetValue(Grid.RowProperty, i);
            // Adds the dynamically created control to the canvas
            LayoutRoot.Children.Add(btnMonday);
       }

I created the button for Monday column, but I can't assume Monday is gettime.now(), may I know how could I get the date for day of current week ?

preguntado el 08 de noviembre de 11 a las 14:11

Are you asking for the DayOfWeek enum? I'm a little confused by your question. -

i know the button is designed for monday, tuesday, wednesday, thursday and friday. but it's custom button, so the button need to know the date of monday of this week, tuesday of this week... -

1 Respuestas

Como DayOfWeek is an enum, it can be casted to an int.

public enum DayOfWeek
{
    Sunday = 0,
    Monday = 1,
    Tuesday = 2,
    Wednesday = 3,
    Thursday = 4,
    Friday = 5,
    Saturday = 6,
}

Then you can take that number of days from DateTime.Today

public static DateTime GetLastSunday()
{
    DateTime today = DateTime.Today;
    return today.AddDays(-((int)today.DayOfWeek));
}

You could then Add the day of the week back again that you want

public static DateTime GetDayOfThisWeek(DayOfWeek dayOfWeek)
{
    DateTime today = DateTime.Today;
    DateTime lastSunday = today.AddDays(-((int)today.DayOfWeek));
    return lastSunday.AddDays((int)dayOfWeek);
}

Actualizar:

As for getting 30 minute chunks:

public IEnumerable<DateTime> GetHalfAnHourChunks(DateTime startFrom, DateTime until)
{
    while(startFrom < until)
    {
        yield return startFrom;
        startFrom = startFrom.AddMinutes(30);
    }
}

respondido 08 nov., 11:20

I'm still testing this and let you know later about the result =D i trust everything should work like a charm. but, may i know do you have any suggestion on how to do the part of determine how many hours between then split into every 30 minute parts ? - 1 myb

DateTime.Now - startOfWeekDateTime will give you time between then and now as a TimeSpan. As for splitting into 30 minute chunks, I'll stick something in the answer (Y) - Connell

Thx dude... My weakness is doing such algorithm @.@ your answer helped a lot =D - 1 myb

No es la respuesta que estás buscando? Examinar otras preguntas etiquetadas or haz tu propia pregunta.