Get the day of the week according to a date in javascript

2

I have the following code of a function, I need to get the day of the week from a day that is sent to the function, so I have it:

    diaSemana : function(d){
      var fecha = new Date();
      var dias = ["L", "M", "X", "J", "V", "S", "D"];

      var mes = fecha.getMonth()+1; //obteniendo mes
      var dia = d;
      var ano = fecha.getFullYear(); //obteniendo año
      if(dia<10)
          dia='0'+dia; //agrega cero si el menor de 10
      if(mes<10)
          mes='0'+mes; //agrega cero si el menor de 10
      var fec = (dia+"/"+mes+"/"+ano);

      return dias[fec.valueAsDate.getDay()];
    }

I want to show next to the day number, the name of the day of the week, for that I send it to the function the day.

I'm a newbie in JavaScript.

    
asked by Venturo Lévano Adrián 26.05.2018 в 01:34
source

1 answer

0

Some things are missing, remember that the week starts on Sunday. Also remember that the dates in English begin with the month. The correct way would be like this:

diaSemana = function(d){
      var fecha = new Date();
      var dias = ["D", "L", "M", "X", "J", "V", "S"];

      var mes = fecha.getMonth()+1; //obteniendo mes
      var dia = d;
      var ano = fecha.getFullYear(); //obteniendo año
      if(dia<10)
          dia='0'+dia; //agrega cero si el menor de 10
      if(mes<10)
          mes='0'+mes //agrega cero si el menor de 10
      var fec = mes+"/"+dia+"/"+ano;
      var day = new Date(fec).getDay();
     
      return dias[day];
    }
    
    console.log(diaSemana(25));
    
answered by 26.05.2018 / 01:43
source