Leszcz Terry
Leszcz Terry

Reputation: 63

ASP NET countdown timer with from Label1.Text

There is possible to start countdown timer with date from any control like Label.Text or TextBox.Text? I found only with a specific date like 2018-10-22 03:42:37 but i need that date from control.

Upvotes: 1

Views: 1165

Answers (3)

Leszcz Terry
Leszcz Terry

Reputation: 63

I found solution Heres my. I dont remeber who made tish but credits for him.

       protected void Page_Load(object sender, EventArgs e)
    {


        if (!Page.IsPostBack)
        {
            SqlConnection con = new SqlConnection("server=localhost; database=DB_TS; trusted_connection=true;");
            con.Open();

            string queryString = @"Select * from Vote";
            SqlCommand cmd = new SqlCommand(queryString, con);

            SqlDataReader dr;
            dr = cmd.ExecuteReader();
            while (dr.Read())
            {
                TextBox1.Text = dr["Data2"].ToString();

            }

            dr.Close();
            cmd.ExecuteNonQuery();

            con.Close();                

        }


        DataTable dt = new DataTable();
        DateTime startDate = DateTime.Now;
        DateTime dt2 = DateTime.Parse(TextBox1.Text);
        DateTime endDate = Convert.ToDateTime(dt2.ToString());
        Label1.Text = CalculateTimeDifference(startDate, endDate);


    }

    public string CalculateTimeDifference(DateTime startDate, DateTime endDate)
    {
        int days = 0; int hours = 0; int mins = 0; int secs = 0;
        string final = string.Empty;
        if (endDate > startDate)
        {
            days = (endDate - startDate).Days;
            hours = (endDate - startDate).Hours;
            mins = (endDate - startDate).Minutes;
            secs = (endDate - startDate).Seconds;
            final = string.Format("{0} days {1} hours {2} mins {3} secs", days, hours, mins, secs);
        }
        return final;
    }

Upvotes: 1

Leszcz Terry
Leszcz Terry

Reputation: 63

I have 2 controls 1 is with DataBind from SQL(When user click is insert DateAdd +12 hour) date and second shold show cutdown I was try this one

https://www.w3schools.com/howto/howto_js_countdown.asp

But this way does not read date from control.

Upvotes: 1

Ajay2707
Ajay2707

Reputation: 5798

you can simply add a timer control and the value of timer you will show to any label

 <asp:timer id="Timer1" runat="server" interval="1000" ontick="Timer1_Tick" xmlns:asp="#unknown"></asp:timer>
<asp:lable id="Lable1" runat="server" xmlns:asp="#unknown" />

Just see the example in this links Countdown timer on ASP.NET page

https://www.youtube.com/watch?v=5yyXtIvyYxc

or you can use javascript if you only show

<script type="text/javascript">

    function countdown() 
    {
        seconds = document.getElementById("timerLabel").innerHTML;
        if (seconds > 0) 
        {
            document.getElementById("timerLabel").innerHTML = seconds - 1;
            setTimeout("countdown()", 1000);
        }
    }

    setTimeout("countdown()", 1000);

</script>

Countdown timer on ASP.NET page

Upvotes: 1

Related Questions