Set DateTime return format

Hi,
Is it possible to set the return (not the display or picker) format of DateTime?

I need the “YYYYMMDD” format, like “19761001”.
By default, the component returns a string like “YYYY-MM-DD”.
I know i can use external libraries like “moment”, but it would be a bit overpriced i think.

Thx

Hi Moscardini,

You can use regular expression for that.
Example:

var date = "1976-10-01";
var newdate = date.replace(/-/g,""); // => "19761001"
1 Like

Date is submitted through a form and I would have liked to avoid changing it.

If there are no other solutions, I will replace the date trying something like that:

this.form.patchValue({date: this.form.value.date.replace(/-/g,'')});
1 Like

There is a saying:

You have a problem. You write a regular expression to solve it. Now you have two problems.

Here is a complete working pipe that uses date-fns/format. You should be able to adapt it easily to do what you want.

2 Likes

Thanks for suggestion.

Thx!

Better than this.form.value.date = this.form.value.date.replace(/-/g, "");