I have a AJAX C# ASP.NET control and it works properly (counts down from 2 hours from current time only when the datetime is hardcoded. I would like to pass in dates as a variable but every time I do so, the timer stops working.
Here's what works...
protected void Timer1_Tick(object sender, EventArgs e)
{
String DateTimeFuture = DateTime.Now.AddHours(2).ToString();
DateTime NeededByDateTime = DateTime.Parse("03/18/2017 10:00:00 AM");
TimeSpan time1 = new TimeSpan();
time1 = NeededByDateTime - DateTime.Now;
if ((time1.Hours == 0) && (time1.Minutes == 0) && (time1.Seconds == 0))
{
Label1.Text = "Time Expired!";
//Return article to results
}
else
{
string countDown = string.Format("{0} Days, {1} Hours, {2} Minutes, {3} Seconds til launch.", time1.Days, time1.Hours, time1.Minutes, time1.Seconds);
Label1.Text = countDown.ToString();
}
}
Here's what doesn't work...
protected void Timer1_Tick(object sender, EventArgs e)
{
String DateTimeFuture = DateTime.Now.AddHours(2).ToString();
DateTime NeededByDateTime = DateTime.Parse(DateTimeFuture);
TimeSpan time1 = new TimeSpan();
time1 = NeededByDateTime - DateTime.Now;
if ((time1.Hours == 0) && (time1.Minutes == 0) && (time1.Seconds == 0))
{
Label1.Text = "Time Expired!";
}
else
{
string countDown = string.Format("{0} Days, {1} Hours, {2} Minutes, {3} Seconds til launch.", time1.Days, time1.Hours, time1.Minutes, time1.Seconds);
Label1.Text = countDown.ToString();
}
}
Any ideas as to why it isn't working for the second piece of code?
UPDATE:
If you would like to try to get the example working for your environment, you'll need the following code in your page.
<form method="post" runat="server" action="Page.aspx">
<asp:ScriptManager ID="ScriptManager1" runat="server" />
<asp:UpdatePanel ID="UpdatePanel1" Visible="true" runat="server" >
<ContentTemplate>
<asp:Timer ID="Timer1" runat="server" Interval="1000" OnTick="Timer1_Tick">
</asp:Timer>
<asp:Label ID="Label1" runat="server"></asp:Label>
</ContentTemplate>
</asp:UpdatePanel>
</form>
contentNeededByDateTime - DateTime.Now;is always about 2 hours.DateTimeFutureis set to a point 2 hours ahead from now.