Angular 6 date and time in English

Asked

Viewed 7,042 times

1

Good afternoon, I’m starting to develop an application in Angular 6, and I need to display the date and time on the screen. I have managed to present both, but they are in the American standard. How can I change to the Brazilian standard?

3 answers

5

I got it. I did this:

import { registerLocaleData } from '@angular/common';
import localeBr from '@angular/common/locales/pt';

In Ngmodule import I also imported LOCALE_ID

Afterward

registerLocaleData(localeBr, 'pt')

And in the declaration of providers

{ provide: LOCALE_ID, useValue: 'pt' }

All this in the main module(appmodule)

  • Yes, I’ve done it in projects here. I didn’t even remember to put in the answer, I thought I just wanted to format it myself.

2

According to the official documentation of Angular, you can use the predefined formats of Datepipe, or if you prefer, customize their formats, I believe, will be indicated for your case. Read more about Datepipe on https://angular.io/api/common/DatePipe

Use as follows:

//Classe do seu componente
class SuaClasse{
  hoje: number = Date.now();
}

//no seu template
<div>
  <p>Data e hora de hoje é {{ hoje | date:'dd/MM/yyyy HH:mm:ss' }}</p>
</div>

I hope I’ve helped!

-1

For this you can use the interpolation followed by the pipe date.

Example in HMTL:

{{ variavelComAData | date: 'dd/MM/yyyy hh:mm:ss' }}

For more details see Datepipe documentation in angular Docs.

Browser other questions tagged

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