Format Angular Date

Asked

Viewed 2,054 times

0

Hello, I have the following string:

Tue Oct 16 2018 00:00:00 GMT-0300 (-03)

And I wanted to format it into dd/mm/yyyy and my goal is to stay 16/10/2018

How I would have to do this formatting process?

2 answers

1

Below is an example of component with Imports to convert dates.

It is important import, place as preview and instantiate to be able to use Datepipe.

import { DatePipe } from '@angular/common'; //import do datePipe

@Component({
    selector: 'app-component',
    templateUrl: './component.html',
    styleUrls: ['./component.css'],
    providers: [DatePipe] //DatePipe como provider
})
export class PesquisaMapaComponent implements OnInit {

    constructor(
            private datePipe: DatePipe //instanciar o DatePipe
    ) { }

    this.datePipe.transform(dataValor, 'dd/MM/yyyy') //um exemplo de uso. isso irá retornar sua data no formato selecionado, no caso 'dd/MM/yyyy
}

0

If you have a recent version (5.5 forward if I am not mistaken), as exemplified in Pipes documentation, you need to use Pipes this way:

<p>The hero's birthday is {{ birthday | date:"MM/dd/yy" }} </p>

In your case I’d be

<p> Data atual é {{ data | date:"dd/MM/yyyy" }} </p>
  • However I want this formatting to make queries in the database, not to print in html.

  • good, for that you need to check what kind of value you are trying to modify, is a Date? you want to pick an already informed date or want the current date with the command new Date().toLocaleString()?

  • The date is stored in a variable now, a variable of type any that is probably being printed in string

  • stores this variable in a type Date and tries to give her a console so: console.log(variavel..getFullYear()). Gives error or back 2018?

Browser other questions tagged

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