获取一周中某一天的可预订日期

时间:2011-11-08 14:17:56

标签: c# datetime silverlight

我正在制作一个允许讲座创建咨询槽的页面,我制作的方式是在循环中填充按钮控制5次(5个工作日)。我需要在时间表中禁用按钮获取类,并使用按钮内容识别按钮然后存储到数据库中。当讲师点击按钮时,会弹出列(日)+时间段的日期。

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

然后我计划使用end_time - start_time = 2小时,如果是2小时,那么08:00,08:30,09:00,09:30 4按钮将从start_time禁用。我用

创建了按钮
        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);
       }

我创建了星期一列的按钮,但我不能假设星期一是gettime.now(),我可以知道如何获取当周某一天的日期?

1 个答案:

答案 0 :(得分:2)

由于DayOfWeek是枚举,因此可以将其转换为int。

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

然后您可以从DateTime.Today

中获取该天数
public static DateTime GetLastSunday()
{
    DateTime today = DateTime.Today;
    return today.AddDays(-((int)today.DayOfWeek));
}

然后,您可以再次添加您想要的星期几

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

<强>更新

至于获得30分钟的块:

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