Probleme d'affichage date et heure sur site

Résolu/Fermé
ophoto Messages postés 68 Date d'inscription jeudi 24 mai 2007 Statut Membre Dernière intervention 2 août 2010 - 23 déc. 2007 à 09:36
 mano - 14 mai 2008 à 09:43
Bonjour,

je crée un site sur lequel je voudrais faire apparaitre la date et heur, j'ai fais ceci :

<script language="javascript">
      function fClock()
      {
      MyClock = new Date;
      hours = MyClock.getHours();
      minutes = MyClock.getMinutes();
      seconds = MyClock.getSeconds();
      day = MyClock.getDate();
      dDate = MyClock.getDay();
      month = MyClock.getMonth();
      year = MyClock.getFullYear();
      tabDate=new Array("Dimanche","Lundi","Mardi","Mercredi","Jeudi","Vendredi","Samedi");
      tabMonth=new Array("Janvier","Fevrier","Mars","Avril","Mai","Juin","Juillet","Aout","Septembre","Octobre","Novembre","Décembre");
      AffMonth = tabMonth[month];
      VarDate = tabDate[dDate]+" "+day +" " + AffMonth + " " + year + "<br>";
      VarDate += hours + " h " + minutes +" : "+ seconds;
      if (document.getElementById){
      document.getElementById("oClock").innerHTML=VarDate;
      }
      setTimeout("fClock()", 500)
      }
      window.onload = fClock;
      </script>
      </p>
      <div align="center" id="oClock"></div>


ca fonctionne parfaitement sauf l'affichage par exemple lorsqu'il est 02 h 05 : 09 ca me note 2 h 5 : 9 comment faire pour que lorsque le chiffre et en dessous de 10 ca me rajoute un "0" devant ?

4 réponses

kiki.boss3 Messages postés 43 Date d'inscription jeudi 26 juillet 2007 Statut Membre Dernière intervention 30 mars 2008 13
23 déc. 2007 à 11:48
il faut faire ajouter le 0 comme suit:
if(hours < 0)
{
hours = "0" + hours;
}
2
ophoto Messages postés 68 Date d'inscription jeudi 24 mai 2007 Statut Membre Dernière intervention 2 août 2010 35
23 déc. 2007 à 12:54
ahhhhhhhh ok je vois mais peux tu me dire où je dois le placer pour heure, minute et seconde
0
kiki.boss3 Messages postés 43 Date d'inscription jeudi 26 juillet 2007 Statut Membre Dernière intervention 30 mars 2008 13
24 déc. 2007 à 14:44
placer le avant l'affichage de l'heure comme suit :
<script language="javascript">
function fClock()
{
MyClock = new Date;
hours = MyClock.getHours();
if(hours < 10)
{
hours = "0" + hours;
}//répéter ça pour tous les autre c-à-d : minutes, seconds (dand le code remplacer hours par minutes que pour seconds )
minutes = MyClock.getMinutes();
seconds = MyClock.getSeconds();
day = MyClock.getDate();
dDate = MyClock.getDay();
month = MyClock.getMonth();
year = MyClock.getFullYear();
tabDate=new Array("Dimanche","Lundi","Mardi","Mercredi","
Jeudi","Vendredi","Samedi");
tabMonth=new Array("Janvier","Fevrier","Mars","Avril","Mai
","Juin","Juillet","Aout","Septembre","O
ctobre","Novembre","Décembre");
AffMonth = tabMonth[month];
VarDate = tabDate[dDate]+" "+day +" " + AffMonth + " " + year + "<br>";
VarDate += hours + " h " + minutes +" : "+ seconds;
if (document.getElementById){
document.getElementById("oClock").innerHTML=VarDate;
}
setTimeout("fClock()", 500)
}
window.onload = fClock;
</script>
</p>
<div align="center" id="oClock"></div>
0
super ca fonctionne merci
0
program pour affichager at é héur
0