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

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.
<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'));

Updated 17-Aug-12 2:24am

Try this

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

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

using Jquery like this

and to pass the value to codebehind you can have a look on Calling a code-behind function from JavaScript[^]
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.
in your caltime function
before return add following line

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

have happy coding
hi guys use this code!!!

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "">

<html xmlns="">
<head runat="server">
    <title>Untitled Page</title>
    <script type="text/javascript" src="js/jquery-1.7.2.min.js"></script>
    <script type="text/javascript">
    var str="";
    var i;
     str = str + $('#Menu li').slice(i).attr('id')+",";
     $("input[id$='TextBox57']").val(str);// if you are using master page use this line is better
    <form id="form1" runat="server">
    <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>
<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--%>

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


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

Let try it...

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