Set DateTime return format


#1

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


#2

Hi Moscardini,

You can use regular expression for that.
Example:

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

#3

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,'')});

#4

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.


#5

Thanks for suggestion.


#6

Thx!

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