Format date in Angular?

Asked

Viewed 1,281 times

0

I am developing a form in which birth date is required:

<div class="form-group">
     <label for="name">Data Nascimento</label>
     <input type="text" placeholder="Insira Data" class="form-control" id="dataNascimento" required [(ngModel)]="user.dataNascimento" name="dataNascimento" bsDatepicker>
</div>

I’m using the library bsDatepicker to Create a Calendar.

Imagery

inserir a descrição da imagem aqui

In the listing appears a strange format: 2018-12-07T05:00:00.000Z the errr date invalidates.

inserir a descrição da imagem aqui

User ts.

export class User {
    id: number;
    nome: string;
    email: string;
    sexo:string;
    nacionalidade: string;
    naturalidade: string;
    cpf: string;
    dataNascimento: string;
}
  • bsDatepicker needs to change to en BR if I’m not mistaken ... is in American format

  • In which part of the code I change it ?

  • https://valor-software.com/ngx-bootstrap/#/datepicker#locales gives a read

1 answer

1


You can use the Datepipe to format dates, in your example it can look like this:

[(ngModel)]="user.dataNascimento | date:'dd/MM/yyyy'"

Of course this is an example, the date format is up to you.

  • In this example you made a mistake... I believe you care about the project. How can I do this ?

  • Julio Neto, the date I’m recording is this: dateNascimento":"2012-12-01T04:00:00.000Z" at list time is coming invalid date

  • 1

    I get it, the component is not recording a string, but an object of the Date type, so when you copy to your datafield which is a string, it copies using a standard javascript date format.

  • 1

    That’s why it’s always important to carefully read the documentation of the components you use. Unfortunately I’ve never worked with that particular component.

Browser other questions tagged

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