How to return the first and last day of the current month in Javascript?

Asked

Viewed 11,376 times

2

I’m having doubts on how to return the first and last day of the current month in JS.

  • 1

    Uai, the first day of any month is always 1

  • Hello @Leocaracciolo, yes exactly what I do in the reply, where I spend the current year and month, and day 1.

3 answers

6

In charge of sharing knowledge, my solution was...

var date = new Date();
var primeiroDia = new Date(date.getFullYear(), date.getMonth(), 1);
var ultimoDia = new Date(date.getFullYear(), date.getMonth() + 1, 0);

console.log(primeiroDia, ultimoDia); /* Conferir no console os dados das referências */

I hope to help.

  • Hello @Isac, to confirm yes, I am switching to a variable that I still use later on my screen. But I will include the console log in the reply to make it more complete. Thank you

5


function formataData(data) {
 var diaS = data.getDay();
 var diaM = data.getDate();
 var mes = data.getMonth();
 var ano = data.getFullYear();
 
 switch (diaS) { //converte o numero em nome do dia
  case 0:
   diaS = "Domingo";
   break;
  case 1: 
   diaS = "Segunda-feira";
   break;
  case 2:
   diaS = "Terça-feira";
   break;
  case 3:
   diaS = "Quarta-feira";
   break;
  case 4:
   diaS = "Quinta-feira";
   break;
  case 5:
   diaS = "Sexta-feira";
   break;
  case 6:
   diaS = "Sabado";
   break;
  }

 switch (mes) { //converte o numero em nome do mês
  case 0:
   mes = "Janeiro";
   break;
  case 1:
   mes = "Fevereiro";
   break;
  case 2:
   mes = "Março";
   break;
  case 3:
   mes = "Abril";
   break;
  case 4:
   mes = "Maio";
   break;
  case 5:
   mes = "Junho";
   break;
  case 6:
   mes = "Julho";
   break;
  case 7:
   mes = "Agosto";
   break;
  case 8:
   mes = "Setembro";
   break;
  case 9:
   mes = "Outubro";
   break;
  case 10:
   mes = "Novembro";
   break;
  case 11:
   mes = "Dezembro";
   break;
  }
 
  if (diaM.toString().length == 1)
      diaM = "0"+diaM;
  if (mes.toString().length == 1)
      mes = "0"+mes;
 
 return diaS + ", " + diaM + "/" + mes + "/" + ano;
}

var d = new Date();
var anoC = d.getFullYear();
var mesC = d.getMonth();

var d1 = new Date (anoC, mesC, 1);
var d2 = new Date (anoC, mesC+1, 0);

console.log(formataData(d1));
console.log(formataData(d2));

  • Hello @Leocaracciolo! Much more complete answer than necessary for my case! I will accept it to help other people with similar problems. Thank you!

1

As I wanted to know only the last working day of the month I used the

var date = new Date();
    var ultimoDia = new Date(date.getFullYear(), date.getMonth() + 1, 0);

My JS code looks like this:

   $(document).ready(function() {
    var date = new Date();
    var ultimoDia = new Date(date.getFullYear(), date.getMonth() + 1, 0);
    $('#fimVigencia').datepicker("setDate", ultimoDia);
});

Note: I used Datepicker

Follow my code in HTML:

    <div class="form-group">
            <div class="col-lg-12">
                <div class="input-daterange" id="datepicker" style="right: 0px;">
                    <label for="inicioVigencia" class="control-label">Válido Até</label> <input class="form-control" id="fimVigencia" th:field="*{fimVigencia}" required="required" />
                </div>
            </div>
        </div>

Browser other questions tagged

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