将时间划分为插槽

时间:2013-11-15 10:53:32

标签: c# asp.net

我为StartTime,EndTime和SlotDuration创建了ListBoxes。

如果StartTime = 9:00AM,

EndTime =下午4:00,

SlotDuration =10分钟

然后我想动态显示标签

上午:

9:00 AM-9:10AM复选框

9:10 AM-9:20AM复选框

9:20 AM-9:30AM ..........等...

下午:

12:00 PM-12:10PM复选框

12:10 PM-12:20PM ................

像那样


<asp:WizardStep ID="WizardStep3" runat="server" Title="Step 3">
    <div class="content">

        <h1>Matchmaking starts at 
            <asp:DropDownList ID="StartTime_DDL" runat="server" Style="color: #727272 !important; font-size: 24px; font-weight: 100;" CssClass="span2">
                <asp:ListItem Text="9:00 AM"></asp:ListItem>
                <asp:ListItem Text="9:30 AM"></asp:ListItem>
                <asp:ListItem Text="10:00 AM"></asp:ListItem>
                <asp:ListItem Text="10:30 AM"></asp:ListItem>
                <asp:ListItem Text="11:00 AM"></asp:ListItem>
                <asp:ListItem Text="11:30 AM"></asp:ListItem>
                <asp:ListItem Text="12:00 PM"></asp:ListItem>
                <asp:ListItem Text="12:30 PM"></asp:ListItem>
                <asp:ListItem Text="1:00 PM"></asp:ListItem>
                <asp:ListItem Text="1:30 PM"></asp:ListItem>
                <asp:ListItem Text="2:00 PM"></asp:ListItem>
                <asp:ListItem Text="2:30 PM"></asp:ListItem>
                <asp:ListItem Text="3:00 PM"></asp:ListItem>
                <asp:ListItem Text="3:30 PM"></asp:ListItem>
                <asp:ListItem Text="4:00 PM"></asp:ListItem>
                <asp:ListItem Text="4:30 PM"></asp:ListItem>
            </asp:DropDownList>
            and ends at 
              <asp:DropDownList ID="EndTime_DDL" runat="server" Style="color: #727272 !important; font-size: 24px; font-weight: 100;" CssClass="span2">
                  <asp:ListItem Text="9:00 AM"></asp:ListItem>
                  <asp:ListItem Text="9:30 AM"></asp:ListItem>
                  <asp:ListItem Text="10:00 AM"></asp:ListItem>
                  <asp:ListItem Text="10:30 AM"></asp:ListItem>
                  <asp:ListItem Text="11:00 AM"></asp:ListItem>
                  <asp:ListItem Text="11:30 AM"></asp:ListItem>
                  <asp:ListItem Text="12:00 PM"></asp:ListItem>
                  <asp:ListItem Text="12:30 PM"></asp:ListItem>
                  <asp:ListItem Text="1:00 PM"></asp:ListItem>
                  <asp:ListItem Text="1:30 PM"></asp:ListItem>
                  <asp:ListItem Text="2:00 PM"></asp:ListItem>
                  <asp:ListItem Text="2:30 PM"></asp:ListItem>
                  <asp:ListItem Text="3:00 PM"></asp:ListItem>
                  <asp:ListItem Text="3:30 PM"></asp:ListItem>
                  <asp:ListItem Text="4:00 PM"></asp:ListItem>
                  <asp:ListItem Text="4:30 PM"></asp:ListItem>
                  <asp:ListItem Text="5:00 PM"></asp:ListItem>
                  <asp:ListItem Text="5:30 PM"></asp:ListItem>
                  <asp:ListItem Text="6:00 PM"></asp:ListItem>

              </asp:DropDownList>. 
                                    <br />
            <br />
            Each slot is
            <asp:DropDownList ID="SlotDuration_DDL" runat="server" Style="color: #727272 !important; font-size: 24px; font-weight: 100;" CssClass="span2" OnSelectedIndexChanged="SlotDuration_DDL_SelectedIndexChanged">

                <asp:ListItem>10</asp:ListItem>
                <asp:ListItem>15</asp:ListItem>
                <asp:ListItem>20</asp:ListItem>
                <asp:ListItem>25</asp:ListItem>
                <asp:ListItem>30</asp:ListItem>
                <asp:ListItem>35</asp:ListItem>
                <asp:ListItem>40</asp:ListItem>
                <asp:ListItem>45</asp:ListItem>
                <asp:ListItem>50</asp:ListItem>
                <asp:ListItem>55</asp:ListItem>
                <asp:ListItem>60</asp:ListItem>
            </asp:DropDownList>
            minutes long. </h1>

    </div>
</asp:WizardStep> 

我的C#代码是:

        DateTime start = DateTime.Parse(StartTime_DDL.SelectedItem.Text);
        DateTime end = DateTime.Parse(EndTime_DDL.SelectedItem.Text);
        double duration = double.Parse(SlotDuration_DDL.SelectedItem.Text);
        string morning = "";
        string afternon = "";
        while (true)
        {
            DateTime dtNext = start.AddMinutes(duration);
            if (start > end || dtNext > end)
                break;
            if (start < DateTime.Parse("12:00 PM"))
            {
                morning += start.ToShortTimeString() + "-" + dtNext.ToShortTimeString() + "<br>";

                Label lbl = new Label();
                lbl.Text = morning;
                CheckBox cb = new CheckBox();

                this.Timediv.Controls.Add(lbl);

                this.Timediv.Controls.Add(cb);

            }
            else
            {
                afternon += start.ToShortTimeString() + "-" + dtNext.ToShortTimeString() + "<br>";
                Label lbl1 = new Label();
                lbl1.Text = afternon;
                CheckBox cb1 = new CheckBox();

                this.Timediv.Controls.Add(lbl1);

                this.Timediv.Controls.Add(cb1);
            }
            start = dtNext;
        }
        if (morning.Length > 0)
            morning = "<div class='priority low'><span><strong>Morning</strong></span></div>" + morning;
        if (afternon.Length > 0)
            afternon = "<div class='priority medium'><span><strong>Afternoon</strong></span></div>" + afternon;

我想在每个Label

之后创建一个checkBox

2 个答案:

答案 0 :(得分:2)

试试这个:

    DateTime start = DateTime.Parse( StartTime_DDL.SelectedItem.Text);
    DateTime end = DateTime.Parse(EndTime_DDL.SelectedItem.Text);
    double duration = double.Parse(SlotDuration_DDL.SelectedItem.Text);
    string morning = "";
    string afternon = "";
    while (true)
    {
        DateTime dtNext = start.AddMinutes(duration);
        if (start > end || dtNext > end)
            break;
        if (start < DateTime.Parse("12:00 PM"))
        {
            morning += start.ToShortTimeString() + "-" + dtNext.ToShortTimeString() + "<br>";
        }
        else
        {
            afternon += start.ToShortTimeString() + "-" + dtNext.ToShortTimeString() + "<br>";
        }
        start = dtNext;
    }
    if (morning.Length > 0)
        morning = "<B>Morning</B><BR>" + morning;
    if (afternon.Length > 0)
        afternon = "<B>Afternon</B><BR>" + afternon;
    Label lbl = new Label();
    lbl.Text = morning + afternon;
    Form.Controls.Add(lbl);

答案 1 :(得分:0)

我可以告诉你如何在插槽的开始和结束时间之间进行迭代。我将它们添加到不同的列表中以分离AM和PM:

var startTime = 9;
var endTime = 16;
var slot = 10;

var time = new DateTime(2000, 1, 1, startTime , 0, 0);
var timeCollectionForAM = new List<string>();
var timeCollectionForPM = new List<string>();
for (int i = startTime; i < endTime; i++)
{
    for (int y = 0; y < 6; y++)
    {
        if (time.ToString("tt") == "AM")
        {
            timeCollectionForAM.Add(time.ToString("t") + " - " + time.AddMinutes(slot).ToString("t"));
        }
        else {
            timeCollectionForPM.Add(time.ToString("t") + " - " + time.AddMinutes(slot).ToString("t"));
        }
        time = time.AddMinutes(slot);
    }
}