Webmaster

Tips
All Tips ]

Date and time in a form

To permanently display the date and time on your web page, simply add a few elements to the HTML code:

First, between the tags <HEAD> and </HEAD>, add the JavaScript code:

<SCRIPT LANGUAGE="JavaScript">
var Compteur = null;
var CompteurTourne = false;

function DemarreHorloge () {
if(CompteurTourne)
clearTimeout(Compteur);
CompteurTourne = false;
AfficheTemps();
}

function AfficheTemps () {
var Temps = new Date();
var TempsLocal = Temps.getTime()+
   (Temps.getTimezoneOffset()-60)*60;
var Maintenant = new Date(TempsLocal);
var Heure = " " + Maintenant.getHours();
var minutes = Maintenant.getMinutes();
var secondes = Maintenant.getSeconds();
Heure += ((minutes < 10) ? ":0" : ":") + minutes;
Heure += ((secondes < 10) ? ":0" : ":") + secondes;
document.Horloge.FenetreHeure.value = Heure;
var AujourdHui = " " + Maintenant.getDate();
var Mois = Maintenant.getMonth()+1;
var Annee = Maintenant.getYear();
AujourdHui += "/" + Mois + "/" + Annee;
document.Horloge.FenetreDate.value = AujourdHui;
Compteur = setTimeout("AfficheTemps()",1000);
CompteurTourne = true;
}
// -->
</SCRIPT>

Then, in the definition for the body of the document, after the command BODY, add the attribute onload, to the other attributes already present. So we have:

<BODY onload="DemarreHorloge()">

Finally, in the body of the document itself, between the <BODY> and </BODY> tags, add:

<FORM name="Horloge" onSubmit="0">
<INPUT type="text" name="FenetreDate" size=12 value="">
</INPUT>
<INPUT type="text" name="FenetreHeure" size=12 value="">
</INPUT>
</FORM>

You now have the date and time in a few clicks:



Discover all Webmaster tips.