0

I have a date which is like this "19/05/2020". I am trying to convert it into "yyyy-MM-dd" format using pipe. So the date should be look like "2020-05-19".

this._datePipe.transform("19/05/2020", 'yyyy-MM-dd');

It is giving me an error saying Unable to convert "19/04/2020" into a date' for pipe 'DatePipe

Also tried converting the string to date but again invalid date message is getting display.

Is there any way so that we can convert this date that is "19/05/2020" to a valid one so that we can perform datepipe on it.

Flask Diploy
  • 29
  • 1
  • 8
  • Have you tried converting the string into a datetime object – marcusshep Feb 09 '21 at 14:41
  • Why a date pipe ? Use [moment](https://momentjs.com/) instead `moment("19/05/2020", 'DD/MM/YYYY').format('YYYY-MM-DD');` – Arnaud Denoyelle Feb 09 '21 at 14:44
  • @marcusshep yes tried converting the date to datetime object but getting invalid date. If the date which I have used is in format "yyyy-MM-dd" then new Date() is working fine but if I am providing it in some another format getting invalid date – Flask Diploy Feb 09 '21 at 15:25
  • @ArnaudDenoyelle We dont want to use third party library. – Flask Diploy Feb 09 '21 at 15:26
  • You need a valid date object in order to use date transformations. The string you have is not a valid date string so you either have to provide a valid date string that you can use, or simply use string manipulation to do what you want. – MikeOne Feb 09 '21 at 15:48

1 Answers1

0

I guess you need to convert your date having a supported formatted string :

For example, "2011-10-10" (date-only form), "2011-10-10T14:48:00" (date-time form), or "2011-10-10T14:48:00.000+09:00" (date-time form with milliseconds and time zone) can be passed and will be parsed

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date/parse

This is valid for Date.parse() or new Date().

So you'll need to parse your string with another method.

This may helps you :

How to convert dd/mm/yyyy string into JavaScript Date object?

TooLiPHoNe.NeT
  • 479
  • 1
  • 5
  • 22