Reputation: 952
I have a query built that uses a start date and end date which are both of type datetime. I then have another variable called withinPastTime that is supposed to take in a string and query based on that many hours back. Example if the user enters 24 it will search within a 24 hour period.
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 view
<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));
Any thoughts?
Upvotes: 0
Views: 75
Reputation: 851
You can switch withinPastTime to be an int then you can use the AddHours() method as so:
DateTime.Now.AddHours(withinPastTime);
You can pass negative values to make it go back in time.
This will also restrict any input to be a valid number, other stings would throw an exception.
Upvotes: 3
Reputation: 23078
I think what you need is AddHours:
DateTime.Now.AddHours(-pastHours)
Upvotes: 2