0

I am using angular js filter to format a date on the controller. A user selects a date from a uib-datepicker-popup. It is this date that I want formatted using ISO 8601 (yyyy-mm-dd) date format. I then log my the date picked by the user and get something like this

Sat Aug 20 2016 00:00:00 GMT+0300 (EAT)

However I then use angular $filter like this

$filter('date')(new Date(vm.my_date), 'yyyy-mm-dd');

and wind up with the date as

2016-00-20

Here is my the code on my controller

        // Logs Sat Aug 20 2016 00:00:00 GMT+0300 (EAT)
        console.log(vm.my_date);

        var converted = $filter('date')(new Date(vm.my_date), 'yyyy-mm-dd');

        // Logs 2016-00-20
        console.log(converted);

What am I doing wrong?

3 Answers 3

2

filter should be like 'yyyy-MM-dd' check this documentation angular filter

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

Comments

1

When you write the mm in your filter the filter thinks that you mean minutes with that.
For the filter to understand the ms need to be UPPERCASE so it understands that you mean month.
Changing the filter from:

var converted = $filter('date')(new Date(vm.my_date), 'yyyy-mm-dd');

to

var converted = $filter('date')(new Date(vm.my_date), 'yyyy-MM-dd');

would fix your issue.

1 Comment

Thanks, I had no idea the fix was just that simple.
0

Why don't you use the ui.bootstrap.dateparser? No need to create a custom filter when there's already one built into ui.bootstrap.datepicker. Angular Directives for Bootstrap

Comments

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.