1 Replies - 844 Views - Last Post: 12 June 2012 - 09:25 PM Rate Topic: -----

#1 trickstar34  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 48
  • Joined: 05-June 09

ASP.Net Date used in Javascript not working right

Posted 12 June 2012 - 09:24 AM

I am trying to make an updating clock thats initiated with the servers time and then uses Javascript to update it. I'm pretty sure my Javascript is correct so thats not the problem. For some reason instead of putting the time in like it should, its just coming up blank in the source code. I know the wording makes it hard to understand so maybe looking at the code will help you with what I am talking about. The following is the whole code:

var clockElement = document.getElementById("Clock");
            var clockZone = "CMT";
            var clockHours = <% DateTime.Now.Hour.ToString(); %>;
            var clockMinutes = <% DateTime.Now.Minute.ToString(); %>;
            var clockSeconds = <% DateTime.Now.Second.ToString(); %>;
            function displayClock(incre) {
                var AMorPM;
                var hours;
                var minutes;
                var increment = incre;
                if (increment == 1) {
                    clockSeconds++;
                    }
                if (clockSeconds > 59) {
                    clockMinutes++;
                    clockSeconds = 0;
                    }
                if (clockMinutes > 59) {
                    clockHours++;
                    clockMinutes = 0;
                    }
                if (clockHours > 23) {
                    clockHours = 0;
                    }
                if (clockHours < 12) {
                    AMorPM = "AM";
                    }
                if ((clockHours < 12) && (clockHours > 9)) {
                    hours = clockHours;
                    }
                if ((clockHours < 10) && (clockHours > 0)) {
                    hours = "0" + clockHours;
                    }
                if (clockHours == 0) {
                    hours = 12;
                    }
                if (clockHours > 11) {
                    AMorPM = "PM";
                    }
                if (clockHours == 12) {
                    hours = 12;
                    }
                if (clockHours > 12) {
                    hours = clockHours - 12;
                    }
                clockElement.innerHTML = hours + " : " + minutes + " " + AMorPM + " (" + clockZone + ")";
            }
            function onloadEvents() {
                displayClock(0);
                setInterval(displayClock(1), 1000);
            }


Here is where the problem is:
            var clockHours = <% DateTime.Now.Hour.ToString(); %>;
            var clockMinutes = <% DateTime.Now.Minute.ToString(); %>;
            var clockSeconds = <% DateTime.Now.Second.ToString(); %>;


Instead of being output like this like it should (the numbers I just typed in to emulate what the codes supposed to do):
            var clockHours = 13;;
            var clockMinutes = 42;
            var clockSeconds = 12;


It gets output into the source code as:
            var clockHours = ;
            var clockMinutes = ;
            var clockSeconds = ;


I really don't understand how its not working because I get no compilation errors or anything so it should display right. Any help on this issue would be greatly appreciated.

Is This A Good Question/Topic? 0
  • +

Replies To: ASP.Net Date used in Javascript not working right

#2 Nakor  Icon User is offline

  • Professional Lurker
  • member icon

Reputation: 444
  • View blog
  • Posts: 1,492
  • Joined: 28-April 09

Re: ASP.Net Date used in Javascript not working right

Posted 12 June 2012 - 09:25 PM

try using

var clockHours = <%= DateTime.Now.Hour.ToString() %>;



instead of

var clockHours = <% DateTime.Now.Hour.ToString(); %>;


Was This Post Helpful? 1
  • +
  • -

Page 1 of 1