Click here to Skip to main content
15,868,016 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hai,

i use the javascript code to get current time from local machine. but i need to get this value in Label and then i stored it sql server. so how can i do this please help me..

i use following script.
JavaScript
<script language="JavaScript">

// function to calculate local time
// in a different city
// given the city's UTC offset
function calcTime(city, offset) {

    // create Date object for current location
    d = new Date();
    
    // convert to msec
    // add local time zone offset 
    // get UTC time in msec
    utc = d.getTime() + (d.getTimezoneOffset() * 60000);
    
    // create new Date object for different city
    // using supplied offset
    nd = new Date(utc + (3600000*offset));
    
    
    // return time as a string
    return "The local time in " + city + " is " + nd.toLocaleString();

}

// get Bombay time
alert(calcTime('Bombay', '+5.5'));

</script>
Posted
Updated 17-Aug-12 1:24am
v3

Try this

C#
<asp:Label ID="Lab" runat="server" />

<script type="text/javascript">
    var value = document.getElementById("<%= Lab.ClientID %>").innerHTML;
    functionX(value);
</script>


using Jquery like this
JavaScript
$("[id$=Lab]")


and to pass the value to codebehind you can have a look on Calling a code-behind function from JavaScript[^]
 
Share this answer
 
Comments
prabaran 17-Aug-12 7:47am    
Hello praba..

Thanks,.. I used your code, label get the value and display the current time in client side. But code behind Label.Text= Empty or label.value=empty so that i couldn't store the value in database
Prabhakaran Soundarapandian 17-Aug-12 7:58am    
To store into database you might be having a method right...access that method in JS and pass the value..The above link will be useful..
If you find this as answer mark as answer/rate this.
hii,
in your caltime function
before return add following line

JavaScript
document.getElementById("<%= lableID.ClientID%>").value=nd.toLocaleString();

have happy coding
 
Share this answer
 
v2
hi guys use this code!!!


XML
<!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>Untitled Page</title>
    <script type="text/javascript" src="js/jquery-1.7.2.min.js"></script>
    <script type="text/javascript">
    $(document).ready(function(){
    var str="";
    var i;
    for(i=0;i<5;i++)
    {
     str = str + $('#Menu li').slice(i).attr('id')+",";
    }
    $('#TextBox1').val('');
    $('#TextBox1').val(str);
     $("input[id$='TextBox57']").val(str);// if you are using master page use this line is better
    });
    </script>
</head>
<body>
    <form id="form1" runat="server">
    <div>
    <ul id="Menu">
  <li id='l1'>list item 1</li>
  <li id='l2'>list item 2</li>
  <li id='l3'>list item 3</li>
  <li id='l4'>list item 4</li>
  <li id='l5'>list item 5</li>
</ul>
<asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
       <%-- <asp:TextBox ID="TextBox1" style="display:none" runat="server"></asp:TextBox>  --%> <%-- u can hide your textbox like this format--%>
    </div>
    </form>
</body>
</html>


now u can get your menu ids in textbox then split it in code behind file and store it in DB!!!

Enjoy!!!!

Regards
sarva
 
Share this answer
 
Oh God,JScript....
I think ...
JavaScript
document.getElementById("labelId").innerText="Hi dude!";
 
Share this answer
 
v2
Comments
prabaran 17-Aug-12 7:17am    
Your answer is right but i use this way. i didn't get above return value in Label
JavaScript
document.getElementById("Button1").value = "hi arun";

Let try it...
 
Share this answer
 
v4

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900