2

I have date field coming from database 2015-10-01 in this format.I want to print date in separate form i.e. I mean to say month as a Oct and date as a 01. I am using below code in my html.

userproduct.Date| cmdate:'dd/MM/yyyy'

I have created cmdate filter like this.

.

filter('cmdate', [
            '$filter', function($filter) {
                return function(input, format) {
                    //console.log(input);
                    //console.log(format);
                    if (input !== null) {
                        //breaks DB date to array
                        var parts = input.split(/[-]/).filter(function(str) {
                            return str !== '';
                        });
                        //Takes date with time and returns array with date at 0th position
                        var date = parts[2].split(/[ ]/).filter(function(str) {
                            return str !== '';
                        });
                    }
                    var finalDate = date[0] + '/' + parts[1] + '/' + parts[0];
                    return finalDate;
                };
            }
        ])

but this is not giving me date like this

1
  • 2
    Why not just userproduct.Date| date:'dd/MM/yyyy' ? Commented Oct 19, 2015 at 6:26

2 Answers 2

1

You can use the built-in filter date:

$scope.theDate = '2015-10-01';

{{theDate | date:'d MMM yyyy'}}

It returns:

1 Oct 2015

Or

{{theDate | date:'MMM d'}}

Returns:

Oct 1

Please refer to this for all the possible combination you have https://docs.angularjs.org/api/ng/filter/date

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

2 Comments

No I don't want like this I want to print like this
How do you want it? Show us what you want to return.
1

You can use

userproduct.Date| date:'MMM d'

It returns

Oct 1

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.