3

I use ui.boostrap for a datepicker,

http://plnkr.co/edit/GfOQmgW85U2aW3YbZO7T?p=preview

I need to format the date like "yyyy/MM/dd" because that's how my RESTapi receives the args. Applying $filter in angular.

http://docs.angularjs.org/api/ng.filter:date

It seems that the problem was solved, however when I change the date in the datepicker, the format date changes to a format like 2014-01-30T00:58:43.000Z

how can I set default format date with this tool?

2
  • Did you try <pre>Selected date is: <em>{{dt | date:'yyyy/MM/dd' }}</em></pre>? Commented Jan 28, 2014 at 12:02
  • 1
    Yes, that's a filter in the view, but I need to format the date before sending to the API (the value in the controller), the first time works great but then return to the default settings. I need change the default format date. Commented Jan 28, 2014 at 13:06

1 Answer 1

7

Since the date is a JS Date object, you'll have to convert it before sending. You can use the datefilter to parse manually before sending:

var datefilter = $filter('date'),
    formattedDate = datefilter($scope.dt, 'yyyy/MM/dd');

See this plunk for an example: http://plnkr.co/edit/EJLDpEojoFnf5QfFt4o6?p=preview

Only alternative I know of, is creating a directive for the value and pushing a function to $parsers like in this example, but that's definitely not easy to combine with the datepicker directive.

I'd suggest to continue using the Date object in JS, and just convert the value before sending to your API.

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks, I solved the problem with something similar to your example. Thank you again!

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.