-2

How I can convert string (dd/MM/yyyy) format to dd/MM/yy in Angular template.

Code:

{{ stringDate | date: 'dd/MM/yy' }}

I am getting this error in console

Unable to convert "30/08/2019" into a date' for pipe 'DatePipe'

P.S - I don't want to create custom pipe.

3
  • its new Date() | date doesn't work with string Commented Sep 26, 2019 at 0:52
  • Possible duplicate of this. stackoverflow.com/questions/43202250/… Commented Sep 26, 2019 at 3:47
  • This is a vague requirement. Why don't you want to create a custom pipe? Parsing the date string in template will make your code inefficient. Commented Sep 26, 2019 at 3:51

3 Answers 3

1

Not pretty, but this will make the pipe happy:

  {{ 
    (stringDate.split('/')[1] 
    + '/' 
    + stringDate.split('/')[0] 
    + '/' 
    + stringDate.split('/')[2])
    | date:'dd/MM/yy' 
  }}

Check the stackblitz

Sign up to request clarification or add additional context in comments.

Comments

0

Try changing 30/08/2019 (dd/MM/yyyy) to 08/30/2019 (MM/dd/yyyy) before passing it to the pipe.

The Pipe is expecting the first part of the date (30) to be the month and as 30 is greater than 12 then it does not know what to do.

Comments

0

parse the date to acceptable dateformats then use the date pipe to convert to required format you can refer the acceptable formats from here https://www.w3schools.com/js/js_date_formats.asp and to get the format as you required parse like this

new Date(parseInt(("30/08/2019").split("/")[2],10),parseInt(("30/08/2019").split("/")[1],10)-1,parseInt(("30/08/2019").split("/")[0],10))

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.