Use $filter to format date in controller

This post demonstrates how to use $filter inside the controller to format a JavaScript Date object before sending it as part of the request to a REST URL.

$filter to the rescue

JavaScript Date objects can be formatted using $filter. No need to look for other JS libaries. Normally, $filter is used in HTML. For instance,

Filters in AngularJS are represented by $filter.

$filter in controller

To use $filter in the controller, consider the following code snippet.

$filter is injected to the controller. The important code is this:

Given a Date object, $filter formats it to ‘yyyyMMdd’. Then, you can use this value to pass to your REST URL.

I am using AngularJS