angularpipeformatdatetimeng2-translate

How to change date format angular2


Hi I want to change date format according to the choosed language, I use ng2-translate and this is my working code but it is static:

<span> {{product[col.field] | date : 'dd-MM-yyyy' >}} </span>

I want to have a format date in a current language, something like this:

<span> {{product[col.field] | date : 'DATE.PIPE' | translate >}} </span>

where in the en.json I have"DATE": { "PIPE": "MM-dd-yyyy" and in it.json I have "DATE": { "PIPE": "dd-MM-yyyy" Is it possible? or is there a way to change format date programmatically?


Solution

  • You just need to put the translation part into brackets:

    {{ product[col.field] | date : ('DATE.PIPE' | translate) }}
    

    If you don't, the date pipe takes 'DATE.PIPE' string as its date format.