Nooh Ahamad
Nooh Ahamad

Reputation: 393

How to obtain the name of a month from a number?

I have an array of numbers representing the index of a month, monthArr = [1, 2, 3 , …] and I want to display it in a table using the ngFor directive. When I try,

<tr *ngFor="let month of monthArr">
   <td>{{ month | date: 'MMMM' }}</td>
</tr>

The only output I get is “January”. I cannot find my mistake, where am I going wrong?

Upvotes: 1

Views: 290

Answers (1)

Rafi Henig
Rafi Henig

Reputation: 6432

DatePipe accepts Date, a number (milliseconds since UTC epoch) or ISO string type as a parameter (Angular - DatePipe), you might want to try modifying your monthArr as following:

public monthArr = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11].map(x => new Date(0, x));

Upvotes: 1

Related Questions