Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Date on html page

Status
Not open for further replies.

Mushead

Programmer
May 8, 2003
3
US
I'm sure this is a simple question but i'm currently have a brain fart.

How do i add the system(current) date to an html page?
 
You can do it with a Server Side Include (SSI) like this:
The local date: <!--#echo var=&quot;DATE_LOCAL&quot; -->
(remember to change the file extension to .shtml or .shtm, whatever your server supports.

You can also do it with JavaScript:

//get the current time
currentTime = new Date()

//access hrs/min/sec/etc with the following methods:
currentTime.getHours()
currentTime.getMinutes()
currentTime.getSeconds()
currentTime.getMonth()
currentTime.getYear()
currentTime.getDay()

Now you can print any or all of those values depending on whether you prefer document.write() or, if you wanted to update it consistantly (like a real clock) with, perhaps, innerHtml().

Hope that helps.
 
Well, this pretty much did the trick for me:

Code:
<!DOCTYPE html 
     PUBLIC &quot;-//W3C//DTD XHTML 1.0 Transitional//EN&quot;
     &quot;[URL unfurl="true"]http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd&quot;>[/URL]

<html xmlns=&quot;[URL unfurl="true"]http://www.w3.org/1999/xhtml&quot;[/URL] xml:lang=&quot;en&quot; lang=&quot;en&quot;>
  <head>
    <meta http-equiv=&quot;Content-Type&quot; content=&quot;text/html; charset=iso-8859-1&quot;></meta>
    <title>JavaScript Sample</title>
  </head>
  <body onLoad=&quot;var TemporalLocation=new Date();URHereForm.CurrentDateValue.value=TemporalLocation.toUTCString();&quot;>
    <h2>Title</h2>
    <p>An entire paragraph of content!</p>
    <p>An entire paragraph of content!</p>
    <p>An entire paragraph of content!</p>
    <form name=&quot;URHereForm&quot;>
      <h6>You're viewing this web page on <input type=&quot;text&quot; id=&quot;CurrentDateValue&quot; size=&quot;50&quot; disabled=&quot;disabled&quot; onfocus=&quot;URHereForm.CurrentDateValue.blur();return false;&quot;></input>.</h6>
      <input type=&quot;button&quot; value=&quot;Update&quot; onClick=&quot;var TemporalLocation=new Date();URHereForm.CurrentDateValue.value=TemporalLocation.toUTCString();return true;&quot;></input>
    </form>
  </body>
</html>

Trouble is, I couldn't get it to automatically populate the field under Mozilla (just IE). I suspect it has something to do with the body onLoad event kicking up before there is an actual object to populate. However, when I installed a 1-second delay, it still didn't work.

So, I put the button in there to make sure my code was correct and the button does the update, too.

I'd sure like to know how to make this work for Mozilla, too. Any ideas?

Cheers,


[monkey] Edward [monkey]

&quot;Cut a hole in the door. Hang a flap. Criminy, why didn't I think of this earlier?!&quot; -- inventor of the cat door
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top