JavaScript does not work

0

Why is not my JavaScript code working? My other codes always work, but this one does not have to pray that it works. I want to put a date clock at the top of the page and I do not want to leave those lines of code inside the index.

<SCRIPT LANGUAGE="javascript">

var now = new Date();
var mName = now.getMonth() +1 ;
var dName = now.getDay() +1;
var dayNr = now.getDate();
var yearNr=now.getYear();
if(dName==1) {Day = "Domingo";}
if(dName==2) {Day = "Segunda-feira";}
if(dName==3) {Day = "Terça-feira";}
if(dName==4) {Day = "Quarta-feira";}
if(dName==5) {Day = "Quinta-feira";}
if(dName==6) {Day = "Sexta-feira";}
if(dName==7) {Day = "Sábado";}
if(mName==1){Month = "Janeiro";}
if(mName==2){Month = "Fevereiro";}
if(mName==3){Month = "Março";}
if(mName==4){Month = "Abril";}
if(mName==5){Month = "Maio";}
if(mName==6){Month = "Junho";}
if(mName==7){Month = "Julho";}
if(mName==8){Month = "Agosto";}
if(mName==9){Month = "Setembro";}
if(mName==10){Month = "Outubro";}
if(mName==11){Month = "Novembro";}
if(mName==12){Month = "Dezembro";}
if(yearNr < 2000) {Year = 1900 + yearNr;}
else {Year = yearNr;}
var todaysDate =(" " + Day + ", " + dayNr + "/" + Month + "/" + Year);

document.write('  '+todaysDate);

</SCRIPT>

<SPAN ID="Clock">00:00:00</SPAN>

<SCRIPT LANGUAGE="JavaScript">
    <!--
var Elem = document.getElementById("Clock");
function Horario(){
    var Hoje = new Date();
    var Horas = Hoje.getHours();
    if(Horas < 10){
        Horas = "0"+Horas;
    }
    var Minutos = Hoje.getMinutes();
    if(Minutos < 10){
        Minutos = "0"+Minutos;
    }
    var Segundos = Hoje.getSeconds();
    if(Segundos < 10){
        Segundos = "0"+Segundos;
    }
    Elem.innerHTML = Horas+":"+Minutos+":"+Segundos;
}
window.setInterval("Horario()",1000);
</SCRIPT>
    
asked by anonymous 21.11.2017 / 01:29

1 answer

1

In order for your code to work, you only have to remove the <!-- that is in the second <script> :

<SCRIPT LANGUAGE="javascript">

var now = new Date();
var mName = now.getMonth() +1 ;
var dName = now.getDay() +1;
var dayNr = now.getDate();
var yearNr=now.getYear();
if(dName==1) {Day = "Domingo";}
if(dName==2) {Day = "Segunda-feira";}
if(dName==3) {Day = "Terça-feira";}
if(dName==4) {Day = "Quarta-feira";}
if(dName==5) {Day = "Quinta-feira";}
if(dName==6) {Day = "Sexta-feira";}
if(dName==7) {Day = "Sábado";}
if(mName==1){Month = "Janeiro";}
if(mName==2){Month = "Fevereiro";}
if(mName==3){Month = "Março";}
if(mName==4){Month = "Abril";}
if(mName==5){Month = "Maio";}
if(mName==6){Month = "Junho";}
if(mName==7){Month = "Julho";}
if(mName==8){Month = "Agosto";}
if(mName==9){Month = "Setembro";}
if(mName==10){Month = "Outubro";}
if(mName==11){Month = "Novembro";}
if(mName==12){Month = "Dezembro";}
if(yearNr < 2000) {Year = 1900 + yearNr;}
else {Year = yearNr;}
var todaysDate =(" " + Day + ", " + dayNr + "/" + Month + "/" + Year);

document.write('  '+todaysDate);

</SCRIPT>

<SPAN ID="Clock">00:00:00</SPAN>

<SCRIPT LANGUAGE="JavaScript">
//   <!--
var Elem = document.getElementById("Clock");
function Horario(){
    var Hoje = new Date();
    var Horas = Hoje.getHours();
    if(Horas < 10){
        Horas = "0"+Horas;
    }
    var Minutos = Hoje.getMinutes();
    if(Minutos < 10){
        Minutos = "0"+Minutos;
    }
    var Segundos = Hoje.getSeconds();
    if(Segundos < 10){
        Segundos = "0"+Segundos;
    }
    Elem.innerHTML = Horas+":"+Minutos+":"+Segundos;
}
window.setInterval("Horario()",1000);
</SCRIPT>

Notice that the code you have in the question just commented on <!-- .

However, you can do what you are trying to do in a much more organized, flexible and short way:

var now = new Date();
var mName = now.getMonth();
var dName = now.getDay() +1;
var dayNr = now.getDate();
var yearNr = now.getFullYear(); //troca de getYear() para getFullYear()

const weekDays = ['Domingo', 'Segunda-feira','Terça-feira', 'Quarta-feira', 'Quinta-feira', 'Sexta-feira', 'Sábado' ];
const monthNames = ['Janeiro','Fevereiro','Março','Abril','Maio','Junho','Julho','Agosto','Setembro','Outubro','Novembro','Dezembro'];

let day = weekDays[dName-1]; //obter o dia da semana com base no array
let month = monthNames[mName]; //obter o nome do mês com base no array
let todaysDate = " " + day + ", " + dayNr + "/" + month + "/" + yearNr;
document.getElementById("today").innerHTML = todaysDate;

const elem = document.getElementById("clock");

setInterval(function(){
    let hoje = new Date();     
    elem.innerHTML = hoje.getHours().toString().padStart(2,"0")+":"+
                     hoje.getMinutes().toString().padStart(2,"0")+":"+
                     hoje.getSeconds().toString().padStart(2,"0");
},1000);
<span id="today"></span>
<span id="clock">00:00:00</span>

Summary of Changes:

  • It is not advisable to use labels and / or uppercase attributes so I have changed most of them
  • document.write is inadvisable and so I created a new <span> to put the date in the format it had
  • I created an array for the days of the week and another for the months to stay organized and simpler
  • I made the zeros logic on the clock based on the padStart of the string that makes it much easier
  • I changed the getYear by getFullYear that already brings the year in 4 digits, not requiring manipulation

I would still like to elaborate a little more, but at least it shows you another way to do the same.

    
21.11.2017 / 02:40