Deepanshu Agrawal
Deepanshu Agrawal

Reputation: 43

Change date format using angular js

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

Upvotes: 2

Views: 875

Answers (2)

Lewis Hai
Lewis Hai

Reputation: 1214

You can use

userproduct.Date| date:'MMM d'

It returns

Oct 1

Upvotes: 1

michelem
michelem

Reputation: 14590

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

Upvotes: 1

Related Questions