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

Popular Posts

Resolving the "n timer(s) still in the queue" Error In Angular Unit Tests

How to Get Norton Security Suite Firewall to Allow Remote Desktop Connections in Windows

Silent Renew and the "login_required" Error When Using oidc-client

Fixing the "Please add a @Pipe/@Directive/@Component annotation" Error In An Angular App After Upgrading to webpack 4

How to Determine if a Column Exists in a DataReader