Is there a way to make the datepipe dynamic so that if it's an American browser the datepipe returns the American format (yyyy/MM/dd) and when it's a European browser it returns the European format (dd/MM/yyyy)?
Thanks
Is there a way to make the datepipe dynamic so that if it's an American browser the datepipe returns the American format (yyyy/MM/dd) and when it's a European browser it returns the European format (dd/MM/yyyy)?
Thanks
I think you should use native JS API Date.prototype.toLocaleDateString() to achieve this goal. See this link.
You can implement your own Angular pipe to use this API.
I know that this may seem nasty but it's way simpler than begging Angular to do it for you:
public renderLocaleDateTime(date: Date, dateStyle = 'medium', timeStyle = 'medium'): string {
var script = 'const date = new Date("' + date + '"); const options = { dateStyle: "' + dateStyle + '", timeStyle: "' + timeStyle + '" }; date.toLocaleString({}, options);';
return eval(script);
}
And then:
<span title="{{ renderLocaleDateTime(date, 'long', 'long') }}">
{{ renderLocaleDateTime(date) }}
</span>
Or even better, make it a pipe.