1

I am attempting to format a dateTime returning from a C# web service, formatted as '2016-01-01T00:00:00' to a standard MM/dd/yyyy format in Angular. The date in question is in a select list.

Here is the AngularJS:

angular.module('myApp', [])
    .controller('ctrl', function ($scope) {
    $scope.options = [{
        name: "Date One",
        date: "2015-01-01T00:00:00"
    }, {
       name: "Date Two",
        date: "2016-01-01T00:00:00"
    }];
});

Here is the html:

<body ng-app="myApp">
    <div ng-controller="ctrl">
        <select class="form-control" ng-model="selectedOption" ng-options="option.name + ' ' + option.date | date:'MM/dd/yyyy' for option in options"></select>
    </div>
</body>

You can view this online at https://jsfiddle.net/5s706vLq/7/

0

2 Answers 2

2

Use

option.name + ' ' + (option.date | date:'MM/dd/yyyy') for option in options

as your ng-options. The parenthesis groups the parameter with the format.

https://jsfiddle.net/fvo31sd2/

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

Comments

1

You just need to add brackets for the date filter in ng-options. See below:

<body ng-app="myApp">
    <div ng-controller="ctrl">
        <select class="form-control" ng-model="selectedOption" ng-options="option.name + ' ' + (option.date | date:'MM/dd/yyyy') for option in options"></select>
    </div>
</body>

2 Comments

I can't tell which answer was first, so based on the idea that last in/first out, which is how I think s/o works, I upvoted you but gave @J. Steen credit for the answer.
@Ron Not that it really matters, but if you want to find out such things in the future you can either sort the answers by "oldest" (the three tabs to the right just under your own question) or you can hover your mouse over the "answered yesterday/last week/whatever"-text above the answerer-portrait.

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.