Formatting Dates in Angular Without Using DatePipe
For formatting dates in HTML, Angular provides DatePipe, but what if you need to format a date within a component, service, or other non-HTML code?
For this, Angular provides the formatDate() function. I was unaware of this function until one of the developers I work with submitted a pull request where he used it. I've often seen developers inject DatePipe into their components when they need to format dates, but formatDate() is the better option for that.
Here's an example:
public formattedDate: string = formatDate(Date.now(), "MM/dd/yyyy", 'en-US');
You can read it about it at the official docs here.
Comments
Post a Comment