How do I display the date/time on my frontpage web pages?

  • Thread starter Thread starter Guest
  • Start date Start date
Why bother? generally people know what date/time it is or can look down
to the right on their screen or at their wristwatch if they desire to
know.
You can however use a javascript to do it.
A search on Google for, javascript date time, should give you plenty of
examples.
 
You could try this JS:

function getTheDate()
{
var now = new Date()

date =
["Sunday","Monday","Tuesday","Wednesday","Thursday","Friday","Saturday"]
[now.getDay()] + ", "
+ ["January","February","March","April","May","June","July",
"August","September","October","November","December"]
[now.getMonth()] + " "
+ now.getDate() + " "
+ now.getFullYear()
return date
}
//------------------------------

function getTheTime()
{
function paddigits(number,digits)
{ var limit = (digits == 2) ? 10
: (digits == 4) ? 1000
: 1
return (number < limit) ? "0" + number : number }
//------------------------------

var now = new Date()
var hours = now.getHours()
var dn = (hours >= 12) ? "PM" : "AM"

hours = (hours == 0) ? 12
: (hours > 12) ? (hours - 12)
: hours

var minutes = paddigits(now.getMinutes(),'2')
var seconds = paddigits(now.getSeconds(),'2')

var time = hours + ":" + minutes + ":" + seconds + " " + dn
return time
}
//------------------------------

function DateTime()
{
var cdate = '<b>'
+ getTheDate() + " "
+ getTheTime() + "</b>"

if (browser.ie4 || browser.ie5)
{document.all.clock.innerHTML = cdate}
else
if (document.getElementById) // dom
document.getElementById("clock").innerHTML = cdate
else
document.write(cdate)
}
//------------------------------

function gettime()
{ setInterval("DateTime()",1000) }
//------------------------------

Place the above code, as is, in a text file e.g. external.js

Place this in the head section of your HTML:
<script type="text/javascript" src="scripts/external.js"></script>

Alter the body tag to:
<body onload = "gettime()">

In the place where you want the date and time place this:
Current Date: <span id="clock"></span>

This creates a time ticker, i.e. updated every second. The time will be on
bold like this
Current Date: Friday, March 25 2005 3:00:45 PM
--
Cheers,
Trevor L.
Website: http://tandcl.homemail.com.au


I choose Polesoft Lockspam to fight spam, and you?
http://www.polesoft.com/refer.html
 
Sorry,

I noticed an error (or at least an oversight)

The code following
if (browser.ie4 || browser.ie5)
{document.all.clock.innerHTML = cdate}
else
if (document.getElementById) // dom
document.getElementById("clock").innerHTML = cdate
else
document.write(cdate)

depends on the existence of a function named browser.

This is it.
var browser = new Browser()
function Browser()
{this.dom = (document.getElementById) ? 1: 0
this.ie4 = (document.all && !this.dom) ? 1: 0
this.ns4 = (document.layers && !this.dom) ? 1: 0
this.ns6 = (this.dom && !document.all) ? 1: 0
this.ie5 = (this.dom && document.all) ? 1: 0
this.ok = (this.dom||this.ie4||this.ns4)
this.platform = navigator.platform

this.is_mac = (this.platform.indexOf("ac") != -1);
}
It should be placed at the top of the same js file (which in my example was
/scripts/external.js)
--
Cheers,
Trevor L.
Website: http://tandcl.homemail.com.au


I choose Polesoft Lockspam to fight spam, and you?
http://www.polesoft.com/refer.html
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Back
Top