Format date with Angular Datepipe

Asked

Viewed 2,019 times

0

I have a date in timestamp format and wanted to format for text in Portuguese, for example: "Wednesday, October 18th, 2017", but I put the pipe "| date:'fullDate'" and it doesn’t work. When I take the toLocaleString("pt-BR") and return a Date in the function it returns me "Wednesday, October 18, 2017". Thank you in advance!

typescript

public dt: Date = new Date();
public getDate(): string {
  return this.dt.toLocaleString("pt-BR");
}

html

<div class="data"><b>{{getDate() | date:'fullDate'}}</b></div>

1 answer

0


There is a project that already has a set of functions intended to work with Data, can give a check on:

http://momentjs.com/

But it also follows an alternative:

$scope.getDate = function() {
    let dt = new Date();

    let month = ['Janeiro', 'Fevereiro', 'Março', 'Abril', 'Maio', 'Junho', 'Julho', 'Agosto', 'Setembro', 'Outubro', 'Novembro', 'Dezembro'];
    let week  = ['Domingo', 'Segunda-feira', 'Terça-feira', 'Quarta-feira', 'Quinta-feira', 'Sexta-feira', 'Sábado'];

    let semana = week[dt.getDay()];
    let mes = month[dt.getMonth()];

    return semana + ', ' + dt.getDate() + ' de ' + mes + ', ' + dt.getFullYear();
}       
  • Solved! Thank you very much!

Browser other questions tagged

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