我有一個內置的查詢,它使用了兩種類型的日期時間的開始日期和結束日期。然後我有另一個名爲withinPastTime的變量,它應該在一個字符串和查詢的基礎上花費很多小時。例如,如果用戶輸入24,它將在24小時內搜索。如何將字符串轉換爲日期時間
C#
withinthepast.Text = History.QueryInfo.WithinPastTime;
startdatetext.Text = DateTime.Parse(History.QueryInfo.StartDate).ToString("M/d/yyyy");
enddatetext.Text = DateTime.Parse(History.QueryInfo.EndDate).ToString("M/d/yyyy");
ASP觀點
<div class="row-fluid">
<div class="span8">
<div class="row">
<div class="span1">
<asp:RadioButton ID="RadioButton1" runat="server" Checked="True" GroupName="DateQuery"/>
</div>
<div class="span11">
<span class="label">Start Date</span><br />
<asp:TextBox ID="startdatetext" runat="server" MaxLength="10"></asp:TextBox>
<asp:Image ID="startimage" runat="server" ImageUrl="Images/calender16.png" />
<!-- note: do not add and ID attribute to the AJAX:CalenderExtender -->
<%-- <ajax:CalenderExtender runat="server" TargetControlID="startdatetext" PopupButtonID="startimage" />--%>
</div>
</div>
<div class="row">
<div class="span1">
</div>
<div class="span11">
<span class="label"> End Date </span><br />
<asp:TextBox ID="enddatetext" Enabled="true" runat="server" MaxLength="10"></asp:TextBox>
<asp:Image ID="endimage" runat="server" ImageUrl="Images/calender16.png" />
<!-- note: do not add and ID attribute to the AJAX:CalenderExtender -->
<%-- <AJAX:CalenderExtender runat="server" TargetControlID="enddatetext" PopupButtonID="endimage" />--%>
</div>
</div>
<br />
OR
<br />
<br />
<div class="row">
<div class="span1">
<asp:RadioButton ID="RadioButton2" runat="server" GroupName="DateQuery"/>
</div>
<div class="span11">
<span class="label">Within the past (hours)</span><br />
<asp:TextBox ID="withinthepast" Enabled="true" runat="server" Text="1"></asp:TextBox>
<asp:Image ID="upimage" runat="server" ImageUrl="Images/arrowup16.png" />
<asp:Image ID="downimage" runat="server" ImageUrl="Images/arrowdown16.png" />
<!-- note: do not add and ID attribute to the NumericUpDownExtender -->
<ajax:NumericUpDownExtender runat="server" Minimum="1" Width="75"
TargetControlID="withinthepast" TargetButtonUpID="upimage" TargetButtonDownID="downimage" />
</div>
</div>
</div>
<div class="span4">
<div class="btn-group btn-group-vertical" data-toggle="buttons-checkbox">
<span class="label" style="margin-top: 5px;">Dates in Range</span>
<asp:CheckBoxList ID="timerange" runat="server" style="border-style:solid;float:left;border-width:1px;margin:10px;" Width="120px">
<asp:ListItem Text="Scheduled At" Selected="True"></asp:ListItem>
<asp:ListItem Text="Scheduled For" Selected="True"></asp:ListItem>
<asp:ListItem Text="Submitted" Selected="True"></asp:ListItem>
<asp:ListItem Text="Started" Selected="True"></asp:ListItem>
<asp:ListItem Text="Completed" Selected="True"></asp:ListItem>
</asp:CheckBoxList>
</div>
</div>
</div>
DateTime withinthepast = Date.Evaluate(m_Owner.QueryInfo.WithinPastTime, DateTime.Now(- **this is where my withinthepastvariable would go telling DateTime.Now how many hours to subtract));
有什麼想法?
正確亨克。我得到的錯誤是運算符' - '不能應用於'string'類型的操作數。我已經在OP中發佈了更多的語法,讓你們知道我在看什麼。 – Tim
唯一的問題是它在類TextBox:WebControl中,所以它必須是一個字符串。因此,我得到一個錯誤「不能隱式轉換類型'字符串'爲'int'。」 – Tim
@Tim所以將字符串轉換爲int。我假設在表單上有一些驗證來將條目限制爲有效的數字,如果不是,這是一個完美的時間來添加一些! –