app
app

Reputation: 207

convert date from yyyymmdd to yyyy/mm/dd formate using angular pipe html

Is there any option available in angular to convert yyyymmdd format date to yyyy/mm/dd directly.

I have tried using angular pipe but it is giving different date.

my date is 20201029

{{date | date:'yyyy-MM-dd'}}

Upvotes: 0

Views: 1105

Answers (2)

SovushkaUtrom
SovushkaUtrom

Reputation: 1

If there is many custom formatting and/or forms in your app, check for ngx-mask. You'll find Pipes in the "Other" section.
If this is the only case, a custom pipe is your solution.

Upvotes: 0

Tibebes. M
Tibebes. M

Reputation: 7548

You need a custom pipe to deal with string.

try doing something like this:

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

@Pipe({
  name: 'customDate'
})
export class CustomDatePipe implements PipeTransform {
  transform(value: string): string {
    const pattern = /(\d{4})(\d{2})(\d{2})/
    const [year, month, day] = value.match(pattern).slice(1)

    return [year, month, day].join('-')
  }
}

Upvotes: 2

Related Questions