How to Format Javascript Date by Naming the Month and Day of the Week

Asked

Viewed 4,840 times

5

Hello.
Have that date: 18/05/17.
And I need to convert her to it: 18 mai (Thu)
May: (Thursday): Thursday
Does anyone know how to do this using Javascript and jQuery?

  • I recommend editing your question by placing a more appropriate title. I fear that it will even be duplicated.

  • @Leonancarvalho fear of duplicity without the link of the question that would be the original is slander.

  • @Renan, I’m by mobile application I can not paste the link here. And other , I’m afraid is not statement.

  • Possible duplicate of: https://answall.com/questions/6526/comorformatr-data-no-javascript

3 answers

12


You must have kept the names of the days of the week and the month. Having this is easy:

var meses = [
  "Janeiro",
  "Fevereiro",
  "Março",
  "Abril",
  "Maio",
  "Junho",
  "Julho",
  "Agosto",
  "Setembro",
  "Outubro",
  "Novembro",
  "Dezembro"
];
var dias = ["domingo", "Segunda", "Terça", "Quarta", "Quinta", "Sexta", "Sábado"]

function formatarData(str) {
  var partes = str.split('/').map(Number);
  var data = new Date('20' + partes[2], partes[1] - 1, partes[0]);
  var diaSemana = dias[data.getDay() % 7];
  var mes = meses[data.getMonth()];
  return [data.getDate(), mes.slice(0, 3).toLowerCase(), '(' + diaSemana.slice(0, 3) + ')'].join(' ');
}




var data = '18/05/17';
console.log(formatarData(data));

Another, simpler option is to use the Locale, or browser defaults to show dates:

function formatarData(str) {
  var partes = str.split('/').map(Number);
  var data = new Date('20' + partes[2], partes[1] - 1, partes[0]);
  return data.toLocaleString([], { weekday: 'short', year: 'numeric', month: 'long', day: 'numeric' });
}




var data = '18/05/17';
console.log(formatarData(data));

  • 1

    Good answer! I had thought to do with dateFormat, but besides having the result in English, it became very complicated.

3

 var meses = [
      "Janeiro",
      "Fevereiro",
      "Março",
      "Abril",
      "Maio",
      "Junho",
      "Julho",
      "Agosto",
      "Setembro",
      "Outubro",
      "Novembro",
      "Dezembro"
    ];
    var days = ['Domingo','Segunda','Terça','Quarta','Quinta','Sexta','Sábado'];
    var data = '18/05/17';
    
    var formatData = data.replace(/(\d{2})(\/)(\d{2})/, "$3$2$1");  
    
    var newData = new Date(formatData);
    
    console.log(newData.getDate() + ' ' + meses[newData.getMonth()] + ' (' +  days[newData.getDay()]+ ')');

    

  • Cool solution, short code.

-3

Javascript does not have this feature natively. I suggest using a library for this, preferably Moment js..

On the link page there are several examples of the use of its function format. At the bottom of the page you can change the language and see the same examples with results in Portuguese.

Don’t forget to download the location file for Moment.js in addition to Portuguese of Portugal or Portuguese of Brazil.

Browser other questions tagged

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