SWITCH CASE is not working as expected

Asked

Viewed 53 times

-2

I made this code in Javascript, but it’s coming out the month as December and not the correct one is February.

function getDataPorExtenso() {
    var data = new Date(),
        dia = data.getDate().toString(),
        diaF = (dia.length == 1) ? '0' + dia : dia,
        mes = (data.getMonth() + 1).toString(), //+1 pois no getMonth Janeiro começa com zero.
        mesF = (mes.length == 1) ? '0' + mes : mes,
        anoF = data.getFullYear();

    switch (mesF) {
        case "01":
            mesF = "Janeiro";
        case "02":
            mesF = "Fevereiro";
        case "03":
            mesF = "Março";
        case "04":
            mesF = "Abril";
        case "05":
            mesF = "Maio";
        case "06":
            mesF = "Junho";
        case "07":
            mesF = "Julho";
        case "08":
            mesF = "Agosto";
        case "09":
            mesF = "Setembro";
        case "10":
            mesF = "Outubro";
        case "11":
            mesF = "Novembro";
        case "12":
            mesF = "Dezembro";
    }

    return "São Paulo, " + diaF + " de " + mesF + " de " + anoF;
}
  • 4

    Each case must have a break, otherwise the switch continues executing the others (the call Fall through - see more here, here and here)

1 answer

2

According to the documentation of the term conditional switch the optional instruction break associated with each case ensures that the program gets out of parole switch as soon as the corresponding instruction is executed and executes the instruction that follows shortly after the switch. Case break is omitted, the program continues execution for the next instruction within switch.

function getDataPorExtenso() {
    var data = new Date();
        dia = data.getDate().toString();
        diaF = (dia.length == 1) ? '0' + dia : dia;
        mes = (data.getMonth()).toString(); 
        mesF = (mes.length == 1) ? '0' + mes : mes;
        anoF = data.getFullYear();

    switch (mesF) {
        case "00":
            mesF = "Janeiro";
            break;
        case "01":
            mesF = "Fevereiro";
            break;
        case "02":
            mesF = "Março";
            break;
        case "03":
            mesF = "Abril";
            break;
        case "04":
            mesF = "Maio";
            break;
        case "05":
            mesF = "Junho";
            break;
        case "06":
            mesF = "Julho";
            break;
        case "07":
            mesF = "Agosto";
            break;
        case "08":
            mesF = "Setembro";
            break;
        case "09":
            mesF = "Outubro";
            break;
        case "10":
            mesF = "Novembro";
            break;
        case "11":
            mesF = "Dezembro";
            break;
    }

    return "São Paulo, " + diaF + " de " + mesF + " de " + anoF;
}

console.log(getDataPorExtenso())

Another way to get the same result with a more compact code is to make use of the method Date.prototype.toLocaleDateString() that returns a string with the representation of part of the date based on the language.
Pass as argument "pt-BR" to indicate the language and culture conventions whose either return and options {dateStyle: "long"} indicating either the full date less the day of the week.

function getDataPorExtenso(cidade = "São Paulo") {
  var data = new Date().toLocaleDateString("pt-BR", {
    dateStyle: "long"
  });
  return `${cidade}, ${data}`;
}

console.log(getDataPorExtenso());

Browser other questions tagged

You are not signed in. Login or sign up in order to post.