Friday, June 4, 2010

Create your countdown clock using asp.net ,ajax,c#

Date time calculation , Create your countdown clock using asp.net ,ajax,c#, format TimeSpan etc.


<%@ Page Language="C#" AutoEventWireup="true" CodeFile="cloc.aspx.cs" Inherits="cloc" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
    <script type="text/javascript" language="JavaScript">

            function DisplayDynamicClock()
            {
                setInterval("__doPostBack('upDynamicClock', '');", 1000);
            }
            window.onload = DisplayDynamicClock;
   
    </script>
</head>
<body>
    <form id="form1" runat="server">
   

<asp:ScriptManager ID="smDynamicClock" runat="server" />

<div style="font-family: Trebuchet MS;">

<asp:UpdatePanel runat="server" ID="upDynamicClock" >
    <ContentTemplate>

        <strong>Current Date and Time is:</strong>
        <asp:Label ForeColor="Red" runat="server" ID="lblDynamicClock" />
        <br />
        <asp:Label ID="Label2" runat="server" Text="Compare To:"></asp:Label>
        <asp:TextBox ID="TextBox2" runat="server">5/4/2010 11:01:25 AM</asp:TextBox>
        <br />
        <asp:Label ID="Label1" runat="server" Text="Label"></asp:Label>
    </ContentTemplate>
</asp:UpdatePanel>

</div>
    </form>
</body>
</html>

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class cloc : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        DateTime fdat;
        lblDynamicClock.Text = DateTime.Now.ToString();
        fdat = Convert.ToDateTime(TextBox2.Text);
        DateTime dt1 = DateTime.Now;
        DateTime dt2 =  Convert.ToDateTime(TextBox2.Text);
        TimeSpan ts = dt1.Subtract(dt2);
        Label1.Text = "Days:-" + (int)ts.Days + "<br/>Hours:-" + (int)ts.Hours + "<br/>Mins:-" + (int)ts.Minutes + "<br/>Seconds:-" + ts.Seconds;
     

    }
}