Javascript code to display date and time doesn't work (running on local code editor)

javascript

#1



I just completed the course on Javascript during which time i coded in the code editor provided by codecademy.
Next thing i did was to try out executing javascript locally. The following is my code in it's entirety:

`<html>
    <head>
    <title>JS prog to display date and time</title>
    </head>
    <body>
        <script type = "text/javascript">
            var today = new Date();
            var day = today.getDay();
            var daylist = ["Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday", "Sunday"];
            console.log("Today is: " + daylist[day]);
            var hour = today.getHours();
            var minutes = today.getMinutes();
            var seconds = today.getSeconds();
            var doe = (hour >= 12)? "AM" : "PM";
            hour = (hour>=12)? (hour - 12) : hour;
            if(hour === 0 && doe === "PM");
            {
                if(minutes == 0 && seconds == 0)
                {
                    doe = "Noon";
                }
                hour = 12;
            }
            else if(hour === 0 && doe ==="AM")
            {
                if(minutes === 0 && seconds ===0)
                    {
                        doe = "Midnight";
                    }
                hour = 12;
            }
            console.log("Current time is: " + hour + doe + ":" + minute + seconds);
        </script>
    </body>
</html>`

When i run this on chrome all i get is a blank page. What's wrong?


#2

@mickey005,
If you use the console.log() in a HTML-environment
you will see the console.log() output in the =console= of the Browser debugger (use F12)

The corrected code

           var today = new Date();
            var day = today.getDay();
            var daylist = [
                "Sunday", "Monday", "Tuesday",
                "Wednesday", "Thursday", "Friday",
                "Saturday", "Sunday"];
            console.log("Today is: " + daylist[day]);
            var hour = today.getHours();
            var minutes = today.getMinutes();
            var seconds = today.getSeconds();
            var doe = (hour >= 12)? "AM" : "PM";
            hour = (hour>=12)? (hour - 12) : hour;
            if(hour === 0 && doe === "PM")
            {
                if(minutes === 0 && seconds === 0)
                {
                    doe = "Noon";
                }
                hour = 12;
            }
            else if(hour === 0 && doe ==="AM")
            {
                if(minutes === 0 && seconds ===0)
                    {
                        doe = "Midnight";
                    }
                hour = 12;
            }
            console.log("Current time is: " + 
            hour +
            doe +
            ":" +
            minutes +
            "." +
            seconds);

#3

I don't think your code is any different than mine.


#4

@mickey005,
What i can remember you were using a non-existing minute variable
and
as a semi-colon-; is an End-of-Statement indicator
a code-line

if(hour === 0 && doe === "PM");

would mean the END of that IF statement structure....!!


#5

thanks a lot. Also, i replaced console.log() with document.write(). i guess console.log() is used for debugging purposes only.

PS: if you could please edit your code replacing console.log(); with document.write(), i'd "accept" the answer.


#6

@mickey005,
That was allready done in my previous post....
You could also use the alert() statement (popup should then be enabled)