Home > ASP.Net, Jquery > Display Server Time and have it refresh every second using jQuery

Display Server Time and have it refresh every second using jQuery


Hi all,

try this example to Display Server Time and have it refresh every second using jQuery

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <script src="http://ajax.aspnetcdn.com/ajax/jQuery/jquery-1.5.js" type="text/javascript"></script>
    <script type="text/javascript">
        $(document).ready(function() {
            $.ajax({
                type: "POST",
                contentType: "application/json",
                url: "Display Server Time and have it refresh every second using jQuery.aspx/GetServerTime",
                dataType: "json",
                success: function(data) {
                    dateTime = new Date(data.d);
                    timefunction();
                    setInterval("timefunction()", 1000);
                },
                error: function(XMLHttpRequest, textStatus, errorThrown) {
                    debugger;
                }
            });
        });
        var dateTime = new Date();
        var hour, hourTemp, minute, minuteTemp, second, secondTemp, monthnumber, monthnumberTemp, monthday, monthdayTemp, year, ap;
        function timefunction() {
            dateTime.setSeconds(dateTime.getSeconds() + 1, 0);
            hourTemp = hour = dateTime.getHours();
 
            minuteTemp = minute = dateTime.getMinutes();
            if (minute.toString().length == 1)
                minuteTemp = "0" + minute.toString();
 
            secondTemp = second = dateTime.getSeconds();
            if (second.toString().length == 1)
                secondTemp = "0" + second.toString();
 
            monthnumberTemp = monthnumber = dateTime.getMonth();
            if ((monthnumber + 1).toString().length == 1)
                monthnumberTemp = "0" + (monthnumber + 1).toString();
 
            monthdayTemp = monthday = dateTime.getDate();
            if (monthday.toString().length == 1)
                monthdayTemp = "0" + monthday.toString();
            year = dateTime.getYear();
            ap = "AM";
            if (hour > 11) { ap = "PM"; }
            if (hour > 12) { hour = hour - 12; }
            if (hour == 0) { hour = 12; }
            if (hour.toString().length == 1)
                hourTemp = "0" + hour.toString();
            document.getElementById('time').innerHTML = monthnumberTemp + "/" + monthdayTemp + "/" + year + " " + hourTemp + ":" + minuteTemp + ":" + secondTemp + " " + ap;
        }
    </script>
</head>
<body>
    <form id="form1" runat="server">
    <span id="time"></span>
    </form>
</body>
</html> 

and in code behind :-

[System.Web.Services.WebMethod]
    public static string GetServerTime()
    {
        DateTime dt = DateTime.Now;
        System.Threading.Thread.CurrentThread.CurrentCulture = new System.Globalization.CultureInfo("en-US");
        dt.ToString("d");
        return dt.ToString("MM/dd/yyyy") + dt.ToString("G").Substring(dt.ToString("G").IndexOf(" "));
    }

Hope this helps

Good Luck

Advertisements
Categories: ASP.Net, Jquery
  1. No comments yet.
  1. No trackbacks yet.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: