Turn Date into String and Format to DD/MM/YYYY using Typing

Asked

Viewed 296 times

1

To send a request to the server, I need to format the parameter dataNascization so that it goes in DD/MM/YYYY format. Right now I’m sending him like this :

Request URL: http://localhost:8083/api/patient-api/? name=Maria&datanascimento=Wed%20Mar%2015%201995%2000%3A00%3A00%20GMT-0300%20(Hor%C3%A1rio%20Padr%C3%A3o%20de%20Bras%C3%Adlia)&size=40&page=0

The Json of the filter looks like this :

{nome: Maria ,  dataNascimento: Wed Mar 15 1995 00:00:00 GMT-0300 (Horário Padrão de Brasília)}

am using Angularjs with Typescript.

1 answer

2


Opa, use the library Moment js. and do it this way:

moment('Wed Mar 15 1995 00:00:00 GMT-0300 (Horário Padrão de Brasília)').format('DD/MM/YYYY')

If you want to do it manually use the following function:

function formatDate(date){
  const dateAux = new Date(date);
  
  return dateAux.getDate() + '/' + (dateAux.getMonth()+1) + '/' + dateAux.getFullYear();
}

const date = formatDate('Wed Mar 15 1995 00:00:00 GMT-0300 (Horário Padrão de Brasília)')
document.getElementById('date').innerHTML = date;
<div id="date"></div>

  • Thanks for the tip, but in case I can’t add a new lib to the project, there is another solution ?

  • 1

    Opa Isaias, I put an example without a lib.

Browser other questions tagged

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