2011-11-08 89 views
0

我正在製作一個頁面讓講座創建諮詢槽,我製作的方式是將按鈕控件循環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 START_TIME,09:30 4按鈕將禁用。我創建

 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(),我可以知道我怎麼能得到當前一週的一天的日期?

+0

你要求'DayOfWeek'枚舉嗎?我有點困惑你的問題。 – Marc

+0

我知道按鈕是爲星期一,星期二,星期三,星期四和星期五設計的。但它是自定義按鈕,所以按鈕需要知道本週的星期一的日期,本週的星期二... – 1myb

回答

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分鐘nks:

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

我仍然在測試這個,並且稍後讓我們知道結果= D我相信每件事都應該像魅力。但是,我可否知道你是否有任何建議,以決定在每隔30分鐘的部分之間分配多少小時? – 1myb

+1

'DateTime.Now - startOfWeekDateTime'將會爲您提供time和now之間的時間作爲'TimeSpan'。至於分裂成30分鐘的塊,我會在答案(Y) – Connell

+0

Thx夥計中插入一些東西...我的弱點是做這樣的算法@。@你的答案幫助了很多= D – 1myb