Click here to Skip to main content
Email Password   helpLost your password?

Introduction

I present to you a simple set of functions to display the client's date and time on your web pages.

<script language="JavaScript" type=text/JavaScript>


var dayarray=new Array ("Sunday","Monday","Tuesday","Wednesday",
               "Thursday","Friday","Saturday")

var montharray=new Array("January","February","March","April","May","June",
               "July","August","September","October","November","December")

function getthedate(){
var mydate=new Date()
var year=mydate.getYear()
if (year < 1000)
year+=1900
var day=mydate.getDay()
var month=mydate.getMonth()
var daym=mydate.getDate()
if (daym<10)
daym="0"+daym
var hours=mydate.getHours()
var minutes=mydate.getMinutes()
var seconds=mydate.getSeconds()
var dn=""
if (hours>=12)
dn=""
if (hours>12){
hours=hours-12
}
if (hours==0)
hours=12
if (minutes<=9)
minutes="0"+minutes
if (seconds<=9)
seconds="0"+seconds
//Hire change font size

var cdate="< small> < font color='ffffff' face='Arial'>"
  +dayarray[day]
  +", "+daym+" "+montharray[month]+", "+year
  +" "+hours+":"+minutes+":"+seconds+" "+dn
  +"  < /small>"

if (document.all)
document.all.clock.innerHTML=cdate
else if (document.getElementById)
document.getElementById("clock").innerHTML=cdate
else
document.write(cdate)
}
if (!document.all&&!document.getElementById)
getthedate()
function goforit(){
if (document.all||document.getElementById)
setInterval("getthedate()",1000)
}



</script>
<SPAN id=clock></SPAN>

To use this in your web pages, just include the above script somewhere, and call function goforit() in your <body> tag. For example:

<body leftMargin=0 topMargin=0 
  onload=goforit() marginwidth="0" marginheight="0">

It's very simple. Use it and enjoy.

You must Sign In to use this message board.
 
 
Per page   
 FirstPrevNext
QuestionDate and time javascript
Anonymous
13:43 6 Sep '05  
Smile How can you get the time to display AM or PM
GeneralCode does not work for me
Zon Dynamics
1:16 26 May '05  
How do you call the function goforit() in your text withing <body></body>?

Do you do <script>document.write(goforit());</script> ?

Also, what is the <span></span> for?

God Bless!
GeneralRe: Code does not work for me
Srdjan Mladenovic
5:19 26 May '05  
You must call goforit() function from <BODY> tag
example:
<BODY vLink=#ffffff aLink=#ffffff link=#ffffff bgColor=#003366 leftMargin=0
topMargin=0 önLoad="goforit()" >
GeneralRe: Code does not work for me
Zon Dynamics
7:20 26 May '05  
Srdjan,

It's   not working for me:   I am displaying the date in the middle of a text.   What do you suggest I call "goforit()"?

For example:
Thanks for being here at my website on <script>document.write(goforit());</script>.   It is always a great pleasure serving you. Come back again.

God Bless!
GeneralRe: Code does not work for me
Srdjan Mladenovic
1:51 27 May '05  
Calling goforit() function have nothing to do with position of date on your web page.You must call this function to start displaying date-time,but position of date or time on your page is designers isue.I you are using dreamweawer do this.
Put cursor on place you want to desplay date,Insert/HTML/Script Object/Script in Content editbox of "Script" dialog copy-paste code from this page and call goforit() function from body tag
GeneralRe: Code does not work for me
Zon Dynamics
8:47 27 May '05  
Srdjan,

That's waht I've been doing and what you're suggesting is not working . . . . they date does not display at all!

How about write the code for displaying the date to let me see exactly what you're talking about. Thank you!

Yes, I'm using dreamweaver software.

God Bless!
GeneralRe: Code does not work for me
Srdjan Mladenovic
12:56 27 May '05  
you canvisit this web page and see example
www.megadata.co.yu
GeneralRe: Code does not work for me
Zon Dynamics
10:23 28 May '05  
Yea, Srdjan Mladenovic!

But how do I insert inside a text and have it work for me?

I don't doubt that it works . . . but how can make it work FOR ME?

God Bless!
GeneralWorks fine
maroasis2000
6:32 9 Mar '04  
I was looking to add date and time in m y web page and I foud this code and it works..Thanks
Blush
GeneralUseful but poor explaination
vernijus
12:29 7 Jan '04  
this article is useful but vith very poor explaination
GeneralYep, I'm confused again
dog_spawn
4:59 25 Oct '03  
In the bottom right of my screen I can already see the time and date. As a bonus I can configure it to display in exactly the way I want. So I don't need to visit a webpage to see the time Confused
GeneralRe: Yep, I'm confused again
Anonymous
9:53 28 Oct '03  
Your are clearly NOT a developer..... otherwise you will see the value of a web application having access to the CLIENTS Date/Time info,and not the Date/Time info of the server where the app sits... specially on apps that require a timestamp to be accurately recorded (like bidding or sign-off on things by due date)
GeneralJavascript is *clientside* btw
dog_spawn
7:48 30 Oct '03  
Mate, this is client side javascript... Blush

Anonymous wrote: Your are clearly NOT a developer
Oh thanks for telling me D'Oh!


Last Updated 24 Oct 2003 | Advertise | Privacy | Terms of Use | Copyright © CodeProject, 1999-2010