Link to home
Start Free TrialLog in
Avatar of aaalife
aaalifeFlag for United States of America

asked on

displaying javascript in label

how can i display datetime logic from javascript into a label on an asp.net page???  i tried the following but the time is displaying in upper left corner, not on the datetimelbl.

thanks

<body>

    <span id="tP">&nbsp;</span>
    <script type="text/javascript">
        function timeSource(){
           x=new Date();
           x.setTime(x.getTime());
           return x;
        }
        function leadingZero(x){
           return (x>9)?x:'0'+x;
        }
        function twelveHour(x){
           if(x==0){
              x=12;
           }
           return (x>12)?x-=12:x;
        }
        function dateEnding(x){
           if(x==1||x==21||x==31){
              return 'st';
           }
           if(x==2||x==22){
              return 'nd';
           }
           if(x==3||x==23){
              return 'rd';
           }
           return 'th';
        }
        function displayTime(){
           window.status=''+eval(outputTime)+'';
           document.title=''+eval(outputTime)+'';
           document.getElementById('tP').innerHTML=eval(outputTime);
           setTimeout('displayTime()',1000);
        }
        function amPMsymbol(x){
           return (x>11)?'pm':'am';
        }
        function fixYear4(x){
           return (x<500)?x+1900:x;
        }
        var dayNames=new Array('Sunday','Monday','Tuesday','Wednesday','Thursday','Friday','Saturday');
        var monthNames=new Array('January','February','March','April','May','June','July','August','September','October','November','December');
        var outputTime="dayNames[timeSource().getDay()]+' '+monthNames[timeSource().getMonth()]+' '+timeSource().getDate()+dateEnding(timeSource().getDate())+' '+fixYear4(timeSource().getYear())+' '+':'+':'+' '+twelveHour(timeSource().getHours())+':'+leadingZero(timeSource().getMinutes())+':'+leadingZero(timeSource().getSeconds())+' '+amPMsymbol(timeSource().getHours())";
       
 if(!document.all){ window.onload=displayTime; }else{ displayTime(); }
        document.getElementById("<%= datetimelbl.ClientID %>").innerText = displayTime();
    </script>


    <form id="form1" runat="server">
    <div>
       
        <asp:Label ID="datetimelbl" runat="server" Style="z-index: 122; left: 707px; position: absolute;
            top: 24px" Text="Label" Width="267px">
        </asp:Label>
Avatar of Elvio Lujan
Elvio Lujan
Flag of Argentina image

and what happens if you try:

document.getElementById("<%= datetimelbl.ClientID %>").value= displayTime();
Avatar of aaalife

ASKER

no help, time still not displaying in label
ASKER CERTIFIED SOLUTION
Avatar of Roopesh_7
Roopesh_7

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of RainMan82
RainMan82

var x = document.getElementById('datetimelbl');

try one of these two:

x.text = displayTime();

x.value = displayTime();
Avatar of aaalife

ASKER

neither of the above solutions worked, still getting time in upper left, not in label.
what are you trying to do? are you trying to use a calendar to select a date and put the selected date into a label?
Avatar of aaalife

ASKER

I'm trying to display the current date time from javascript in an asp.net label control.
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of aaalife

ASKER

once i moved script below the label in the body, everything worked fine.  i guess the getelementid needed to already have the label created before finding it.  i moved the code above the label declaration and nothing, with script below, label shows javascript output.