Javascript date and HTML

I'm very new to Javascript. I'm not exactly sure how to call it or use it. And some websites aren't that much of a help. So I thought you guys would be a great help.

I have this so far:

<!DOCTYPE html>
<html>
<head>
    <META HTTP-EQUIV="refresh" CONTENT="2">
    <link rel="stylesheet" type="text/css" href="style.css">
    <title>Current Time</title>
</head>
<body>
    <div class="navBar">
        <a class="nav" href="http://www.blah.com">blah</a>
        <a class="nav" href="http://www.blah2.com">blah2</a>
        <a class="nav" href="https://www.blah3.com">blah3</a>
        <script type="text/javascript">
            function getBostonDate(){
                var currentDate = new Date();
                var dateTime = "Boston current time: " + currentDate.getHours() + ":" + currentDate.getMiutes() + ":" + currentDate.getSeconds();
                document.write(dateTime);
            }
        </script>
    </div>
</body>
</html>

As you can see, I want it to just display a couple of links in the header, and then display the date. (I am in Boston according to my profile so I used that.)

I also tried to put it in it's own .js file and calling it externally and placing that line of code in the part of the HTML. But even then, I have no idea how to call it in the place I want it to be called (after the links). Please help?

Thanks in advance!

Answers


Add a div to your code where you want to display the time, such as

<div id="display_time"></div>

and then, instead of document.write... use:

document.getElementById("display_time").innerHTML=dateTime;

If you want your time to be updated constantly, use setInterval, as in:

setInterval( 
    \\YOUR FUNCTION HERE
,1000);

That will recalculate the time and redisplay it every 1000 ms (=1s).

EDIT: As mentioned by Jon P, you have to call the function (and not only define it). A way to do it is to call it once the page body has loaded. All you have to do is adding it to your body tag as in:

<body onload="setInterval(getBostonDate(),1000);">

Alternatively, you can just call it inside your script, as in

setInterval(getBostonDate(),1000);

Also, as mentioned by Samurai, don't forget to correct your typo: getMinutes() instead of getMiutes()


Need Your Help

asp.net error form cannot be nested within element form?

asp.net forms

I have a content page in an asp.net application that uses a form tag. There's only one on the page so I'm confused why its give me the error: Validation (HTML5): Element 'form' must not be nested w...

Rounding float/decimal to 3 decimal places exactly?

java math rounding precision

I know there are many questions asking for this, but I have tried quite a few of the solutions but none seem to work exactly how I need it!

About UNIX Resources Network

Original, collect and organize Developers related documents, information and materials, contains jQuery, Html, CSS, MySQL, .NET, ASP.NET, SQL, objective-c, iPhone, Ruby on Rails, C, SQL Server, Ruby, Arrays, Regex, ASP.NET MVC, WPF, XML, Ajax, DataBase, and so on.