Javascript Formatting of Dates

Asked

Viewed 48 times

1

could you help me please?

I would like to use the function below in a logic that I am creating, but I would need the function below to return to me only the last week of the month, I am not able to understand how to carry out the proper changes in it so that it works the way I want, if you can help me I appreciate.

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));

  • What do you want? List the dates of the last week of the month from Monday to Friday?

  • That’s right @Marcondes, I want to list only the last week of the month.

  • Take the last day of the month and see if it’s a Friday, if it’s not down one and test again. Until you find the date of that month which it is. After that you know that from that point -4 will be the dates of the last week of the month, it serves?

  • Tell me the last week of July you’ve been waiting for.

1 answer

0


See if it helps.

var datateste = new Date(218,06,27,0,0,0,0); //data, aqui tem que pegar a última data de uma semana cheia do mês atual.
var primeiro = datateste.getDate() - datateste.getDay(); // Primeiro dia é o dia do mês - o dia da semana, contando o Domingo 
var ultimo = primeiro + 6; // primeiro dia + 6, para pegar o sábado
var dataInicial = new Date(datateste.setDate(primeiro));//aqui você pode fazer primeiro + 1 se quer iniciar somente pela segunda-feira
dataInicial = "" + (dataInicial.getMonth() + 1) + "/" + dataInicial.getDate() + "/" + dataInicial.getFullYear();
var dataFInal = new Date(datateste.setDate(ultimo));
dataFInal = "" + (dataFInal.getMonth() + 1) + "/" + dataFInal.getDate() + "/" + dataFInal.getFullYear();
  • From Sunday to Saturday. You can change this to var last = first + 5; or 4 start on Monday.

  • 1

    Man, that’s just what I needed, thanks for the force, I’ll be implementing in my development.

  • Who is last ?

  • adjusted @Leocaracciolo

  • @Gabrielpaixãojustino then gave a good vote on the answer.

Browser other questions tagged

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