Monday, February 27, 2012

Update user Defined Datetime in Javascript

 Add This Source Code to inside Head Tag of your page.

   <script type="text/javascript">

        function GetSETClock() {
            //debugger;
            var d = "";
            if (document.getElementById('clockbox').innerHTML != "") {
                d = new Date(document.getElementById('clockbox').innerHTML);
            }
            else {
// Change this  'mSTime' according to your Requirement
                d = new Date('<%=mSTime%>');
            }
            var nmonth = d.getMonth() + 1;
            var ndate = d.getDate();
            var nyear = d.getYear();
            var nhour = d.getHours();
            var nmin = d.getMinutes();
            var nsec = d.getSeconds();
            //var Now_J =                                  // day of year (Julian day)
            //              Math.round((                          // ...account for DST
            //              (new Date(Now_Y, 12, 31))         // ...most recent midnight
            //              - (new Date(Now_Y, 0, 0)))              // ...Dec. 31st midnight
            //              / 86400000);
            nsec = nsec + 1;
            if (nsec >= 60) {
                nsec = nsec - 60;
                nmin = nmin + 1;
            }
            if (nmin >= 60) {
                nmin = nmin - 60;
                nhour = nhour + 1;
            }
            if (nhour == 24) {
                ndate = ndate + 1;
            }
            nhour = ((nhour >= 24) ? nhour - 24 : nhour);
            var DA_Offsets = new Array(31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31);
            if ((nyear % 400 == 0)
            || ((nyear % 100 != 0) && (nyear % 4 == 0))) {
                DA_Offsets[1] = 29;                     // leap year: Feb=29 days
            }
            if (DA_Offsets[nmonth - 1] < ndate) {
                ndate = 1;
                nmonth = nmonth + 1;
            }
            if (nmonth > 12) {
                nmonth = '1';
                nyear = nyear + 1;
            }
            if (nsec < 10) {
                nsec = "0" + nsec;
            }
            if (nmin < 10) { nmin = "0" + nmin; }
            nhour = ((nhour < 10) ? "0" + nhour : nhour);
            ndate = ((ndate < 10) ? "0" + ndate : ndate);
            nmonth = ((nmonth < 10) ? "0" + nmonth : nmonth);
            document.getElementById('clockbox').innerHTML = nmonth + "/" + ndate + "/" + nyear + " " + nhour + ":" + nmin + ":" + nsec;

        }
Add Refrence Of  'jquery-1.4.1.min.js' in your page

<script src="jquery-1.4.1.min.js" type="text/javascript"></script>
        $(document).ready(function() {
            setInterval("GetSETClock()", 1000);

        });
    </script>


Add Div Tag inside Body Tag

<div id="clockbox">
    </div>

On Server Side Before Page_Load Write This Line-

 public string mSTime = DateTime.Now.ToString();