Angular moment convert DDMMYYYY/MMDDYYYY/YYYYMMDD to Any format(DD/MM/YYYY)

208 views Asked by At

I am using angular mat date input, at MomentDateAdapter parse date I want to convert input number to a specific date format. eg

     |  Input |InpurtFormat|OutputFormat|  Output  |
     |-------:|-----------:|-----------:|---------:|
     |21102012|  DDMMYYYY  | DD/MM/YYYY |21/10/2012|
     |12212020|  MMDDYYYY  | MM/DD/YYYY |12/21/2020|
     |20201221|  YYYYMMDD  | YYYY/MM/DD |2020/12/21|
    parse(value: any, parseFormat: string | string[]): Moment | null {
        if (value && typeof value === 'string') {
            const format = this._dateTimeService.getFormat();
            return moment.utc(value, format, this.locale, true);
        }
        return value ? moment.utc(value).locale(this.locale) : null;
    }
1

There are 1 answers

2
mkHun On

With moment you can do it like this

  const x = moment(date).format("D/M/yyyy");