Raj
Raj

Reputation: 4089

How to use AngularJS date filter with a string that has a date

I have the following files.

index.js

...
completedTooltip = 'Completed on 2019-02-02T23:59:59-07:00';
...

index.html

...
{{completedTooltip | date : 'MM/dd/yyyy'}}
...

The date doesn't get formatted here and just spits out the string.

Is there any way I can make this work OR do I have to just have 2 separate vars so that one var can hold the text and the other can hold the date?

Upvotes: 1

Views: 90

Answers (1)

Bilal Siddiqui
Bilal Siddiqui

Reputation: 3629

You can create custom filter,

app.filter("anyName", function($filter) {
  return function(input, format) {
    var txtArr = input.split(' ');
    txtArr[2] = $filter('date')(txtArr[2], format);
    return txtArr.join(' ');
  };
});

And use that in your HTML

{{completedTooltip | anyName : 'MM/dd/yyyy'}}

Upvotes: 2

Related Questions