0

I am looking for a way to toggle between different date formatters (e.g. mm-dd-yyyy / dd-mm-yyyy) on the ngbDatepicker control based on the user selection.

Any ideas/help is appreciated.

Cheff
  • 79
  • 1
  • 3

1 Answers1

1

Of course (impossible is nothing). But before is necesary you undestand DateParserFormater and DateAdapter see this question in stackoverflow

In your case, you only need work with a CustomDateParseFormat

Well, the only thing that we need is that the functions parse/format and fromModel/toModel depending form one variable. I don't found a great way also inject a service

@Injectable({
  providedIn: 'root',
})
export class MaskController{
  mask:string="yyyy/MM/dd"
  public setMask(mask)
  {
    this.mask=mask;
  }
}

And our CustomDateParserFormatter inject in constructor

@Injectable()
export class CustomDateParserFormatter {
  constructor(private maskController:MaskController){}
  get mask()
  {
    return this.maskController.mask;
  }
   ...
}

well, the only thing we need is change our function depending the value of "mask", and when call to

maskController.setMask(...)

Our ngbDatePicker work as we want. Y put an example in stackblitz (only two "mask" allowed dd/MM/yyyy and yyyy/MM/dd)

NOTE:In the stackblitz I change the dateFormat too. Simply not included in provider the CustomDateAdapter

Eliseo
  • 50,109
  • 4
  • 29
  • 67