I have a timer that shows when an event is scheduled to start and end, but when the set time for the event has been reached, the timer disappears.
I tried to continue to show timer so that it will display the remaining time for the event to end but it did not work
The event ended message shows. The timer during the event that did not show.
The Timer shows before the event, but when the event starts, the timer is not showing
Code:
private void hidecontrols()
{
DataTable dt = new DataTable();
dt.Columns.Add("UserId");
dt.Columns.Add("EventName");
dt.Columns.Add("Event_URL");
dt.Columns.Add("StartDate");
dt.Columns.Add("EndDate");
dt.Rows.Add(1, "Board of Directors", "http://localhost:60223/vote.aspx?id=dAFBBsw9", "2021-10-16 10:00", "2021-10-16 16:00");
DataSet ds = new DataSet();
ds.Tables.Add(dt);
if (ds.Tables[0].Rows.Count > 0)
{
DateTime startTime = Convert.ToDateTime(ds.Tables[0].Rows[0][3].ToString());
DateTime endTime = Convert.ToDateTime(ds.Tables[0].Rows[0][4].ToString());
if (startTime > DateTime.Now)
{
controls.Visible = false;
LabelTime.Text = CalculateT(DateTime.Now, startTime);
Div1.Visible = true;
Div2.Visible = false;
}
else if (endTime > DateTime.Now)
{
controls.Visible = true;
lblTime.Text = CalculateT(endTime, DateTime.Now);
Div1.Visible = false;
Div2.Visible = true;
timer1.Enabled = true; //timer1 has been enabled here to display when the event starts
}
else
{
controls.Visible = false;
Div1.Visible = true;
Div2.Visible = false;
LabelTime.Text = "Event Has Ended";
Label3.Visible = false;
}
}
}
public string CalculateT(DateTime startTime, DateTime endTime)
{
int days = 0; int hours = 0; int mins = 0; int secs = 0;
string finalTime = string.Empty;
if (endTime > startTime)
{
days = (endTime - startTime).Days;
hours = (endTime - startTime).Hours;
mins = (endTime - startTime).Minutes;
secs = (endTime - startTime).Seconds;
finalTime = string.Format("{0} days {1} hours {2} mins {3} secs", days, hours, mins, secs);
}
return finalTime;
}