角度4 dateFormat条件检查

时间:2019-05-15 06:05:05

标签: angular

[locale]="language"标签中,不同的语言如fr,en ...

我要检查如果language ='fr',然后dateFormat =“ MM / DD / YYYY” ,并且如果语言='en',则检查dateFormat =“ YYYY / MM / DD” 等。我如何在dateFormat标记中使用4号角检查?

我已经尝试过了,但是没有用。

<owl-date-time id="emailDateTime" [placeHolder]="'Date of the first export'" [locale]="language"  [disabled]="isNowOnly" [hideClearButton]="isNowOnly" dateFormat="language=='fr'?MM/DD/YYYY:DD/MM/YYYY" [(ngModel)]="moment" [min]="minDate" [ngClass]="['form-control', dateErrorMessage != '' ? 'reporting-form-error-border': '',isNowOnly?'disabled':'']" [inputStyleClass]="isNowOnly?'disabled':''" [ngModelOptions]="{standalone: true}"></owl-date-time>


<owl-date-time id="emailDateTime" [placeHolder]="'Date of the first export'" [locale]="language"  [disabled]="isNowOnly" [hideClearButton]="isNowOnly" dateFormat="DD/MM/YYYY" [(ngModel)]="moment" [min]="minDate" [ngClass]="['form-control', dateErrorMessage != '' ? 'reporting-form-error-border': '',isNowOnly?'disabled':'']" [inputStyleClass]="isNowOnly?'disabled':''" [ngModelOptions]="{standalone: true}"></owl-date-time>

2 个答案:

答案 0 :(得分:0)

您可以尝试

html

<owl-date-time [dateFormat]="getDateFormat(language)"></owl-date-time>

ts

getDateFormat(lang) {
   if (lang === 'fr') {
      return 'MM/DD/YYYY';
   } else {
      return 'YYYY/MM/DD';
   }
}

答案 1 :(得分:0)

您应该使用管道将其转换为所需的格式

import { Pipe, PipeTransform } from "@angular/core";

@Pipe({
    name: "date"
})
export class DatePipe implements PipeTransform {
    transform(value: Date, format: string): string {
        if (value) {
            //use logic get locale format here
            if (format) {
                //then return your locale date here
            }
        }
    }
}