13

I want to format dates on the client-side using angular date filter. I'd like to do that way, because I use angular in some places of my application and I'd like my dates to be formatted uniformly in the whole app.

What I'm trying to do is:

function formatDatetime(date, format) {
  var ngDateFilter = angular.getDateFilter(); //that's what I'm asking about
  return ngDateFilter(date, format);
}

I use angular applications only on several pages, but dates are spread over various pages (with and without angular app).

1 Answer 1

17

OK, as usual I've found an answer. I did:

angular.injector(["ng"]).get("$filter")("date");

and it's OK.

EDIT

As meze observed, I could have also used:

angular.injector(["ng"]).get("dateFilter")

An example with number filter for currency:

var filter = angular.injector(["ng"]).get("$filter")("number");

$('#Price').val(filter(price));
Sign up to request clarification or add additional context in comments.

2 Comments

I think you can do angular.injector(["ng"]).get("dateFilter"); but not sure.
Thanks! Here's a more accurate usage for currency: var filter = window.angular.injector(['ng']).get('$filter')('currency'); var formatted = filter(price,'$')

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.