0

I am using Datepicker control https://ng-bootstrap.github.io/#/components/datepicker/overview. I am using CustomDateParserFormatter to parse the date to different format. My issue is I am using dayjs to transform the dates which intern returns the date as the javascript object which means month will returned from 0 to 11, in that case datepicker shown november in the month dropdown if I enter 12/12/2012. Is there anywhere in the datepicker I can mention that the month starts from 0 and not 1 ? right now I am adding subtracting month which doesn't look clean.

current custom parser

  parse(value: string | null): NgbDateStruct | any {
    const dateFormat = 'MM/DD/YYYY';
    if (value) {
      const otherthing = dayjs(value).format(dateFormat);
      const tests = dayjs(otherthing);
      const something = {
        day: tests.date(),
        month: tests.month() + 1,
        year: tests.year(),
      };
      return something;
    }
    return null;
  }

  format(date: NgbDateStruct): string {
    if (date) {
      const something = dayjs(new Date(date.year, date.month - 1, date.day));
      return something.format('MM/DD/YYYY');
    }
    return '';
  }
threeleggedrabbit
  • 1,722
  • 2
  • 28
  • 60
  • https://stackoverflow.com/questions/56685403/how-to-change-ngbdatepicker-date-format-from-json-to-yyyy-mm-dd/56686978#56686978. Tip: use `let parts = value.replace(/\.|-/g, "/").split('/');` to allow write e.g. 16.2.2020 or 16-02-2020 too – Eliseo Oct 19 '20 at 13:52
  • actually ours is a globalized application working with multiple formats. so I am looking to use the library which I am successful only thing I am trying is to make it much cleaner. – threeleggedrabbit Oct 19 '20 at 14:14
  • Maybe create an `NgbDateAdapter` that does the conversion automatically and use that. But that seems like overkill for what you want. – Ouroborus Oct 19 '20 at 14:24

0 Answers0