David
David

Reputation: 1

Angular date format international

I need to format "item.value.date" as dd/MM/yyyy on a "date.component.html" regardless of the international settings on the local machine. The code below does work on Angular 2, but does not on Angular 4: some dates are wrong. Request: I need a solution for Angular 4 (npm v5.3.0; node v6.11.2)

 <tbody *ngIf="Result" 
 <template ngFor let-item [ngForOf]="Result"> 
     <td {item.value.date  | date:Format}}</td>  
 </template> 
 </tbody> 

On "date.component.ts":

 get Format(): string { 
 return "dd/MM/yyyy "; 
 } 

On "app.module.ts":

import { LOCALE_ID } from '@angular/core'; // for LOCALE_ID
@NgModule({   declarations: [DateComponent],   
imports: [BrowserModule,FormsModule],   
providers: [ { provide: LOCALE_ID, useValue: "en-US" } ] }) 
export class AppModule { }

Upvotes: 0

Views: 845

Answers (1)

Vega
Vega

Reputation: 28738

You can do it with a custom format method which doesn't need to read locals:

HTML:

   <td {{myFormat(item.value.date)}}</td>  

Typescript:

   myFormat(date): string { 
      return ('0' + date.getDate()).slice(-2)+ "/" + ('0' + (date.getMonth() + 1)).slice(-2) + '/' + date.getFullYear()
   }

Upvotes: 0

Related Questions