Assign value to TextBox on page load using JavaScript or jQuery

Last Reply on Dec 13, 2016 11:27 PM By maideen

Posted on Dec 13, 2016 02:53 AM

Mr.Mudassar

I have some javascript code to find the local computer date and time. because our server in US. We are in Malaysia. It picks up the US Time. Below code is display on page only. I need to store the result into mssql database table  so i need to display the result in text box control.

Pls advice me.

thank you

Maideen

<script type="text/javascript">
   var currentTime = new Date()
   var month = currentTime.getMonth()
   var day = currentTime.getDate()
   var year = currentTime.getFullYear()
   var hours = currentTime.getHours()
   var minutes = currentTime.getMinutes()
   var seconds = currentTime.getSeconds()
   var miliseconds = currentTime.getMilliseconds()

   if (minutes < 10) {
       minutes = "0" + minutes
   }
   if (seconds < 10) {
       seconds = "0" + seconds
   }
   document.write(year + "-" + month + "-" + day + " " + hours + ":" + minutes + ":" + seconds + ":" + miliseconds)
</script>

 

 

Posted on Dec 13, 2016 06:50 AM Modified on on Dec 13, 2016 06:56 AM
 
Refer the below code.
 
Using JavaScript:
 
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
    <script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js"></script>
    <script type="text/javascript">
        function GetDateTime() {
            var currentTime = new Date()
            var month = currentTime.getMonth() + 1
            var day = currentTime.getDate()
            var year = currentTime.getFullYear()
            var hours = currentTime.getHours()
            var minutes = currentTime.getMinutes()
            var seconds = currentTime.getSeconds()
            var miliseconds = currentTime.getMilliseconds()
            if (minutes < 10) {
                minutes = "0" + minutes
            }
            if (seconds < 10) {
                seconds = "0" + seconds
            }
            //document.write(year + "-" + month + "-" + day + " " + hours + ":" + minutes + ":" + seconds + ":" + miliseconds)
            document.getElementById('txtDateTime').value = year + "-" + month + "-" + day + " " + hours + ":" + minutes + ":" + seconds + ":" + miliseconds;
        }       
    </script>
</head>
<body onload="return GetDateTime();">
    <form id="form1" runat="server">
    <div>
        <input name="txtDateTime" type="text" id="txtDateTime" />
    </div>
    </form>
</body>
</html>
Demo
 
Using jQuery:
 
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
    <script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js"></script>
    <script type="text/javascript">
        $(function () {
            var currentTime = new Date()
            var month = currentTime.getMonth() + 1 // Need to add 1 to get curent month because the range is [0-11]
            var day = currentTime.getDate()
            var year = currentTime.getFullYear()
            var hours = currentTime.getHours()
            var minutes = currentTime.getMinutes()
            var seconds = currentTime.getSeconds()
            var miliseconds = currentTime.getMilliseconds()

            if (minutes < 10) {
                minutes = "0" + minutes
            }
            if (seconds < 10) {
                seconds = "0" + seconds
            }
            //document.write(year + "-" + month + "-" + day + " " + hours + ":" + minutes + ":" + seconds + ":" + miliseconds)
            $('[id*=txtDateTime]').val(year + "-" + month + "-" + day + " " + hours + ":" + minutes + ":" + seconds + ":" + miliseconds);
        });
    </script>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <input name="txtDateTime" type="text" id="txtDateTime" />
    </div>
    </form>
</body>
</html>

Demo