-1

I need to do convert 09/29/2017 in 2017/09/29. And for this i used this line of code

$scope.dateAsString = $filter('date')($scope.curdate, "yyyy-MM-dd"); 

But unable to convert it.Can anyone tell me where i am wrong?One more thing it's working for new Date().

Any help will be appreciated.Thanks in advance.

user6891871
  • 164
  • 1
  • 3
  • 17
  • Possible duplicate of [How to change date format from mm/dd/yyyy to dd/mm/yyyy?](https://stackoverflow.com/questions/35595949/how-to-change-date-format-from-mm-dd-yyyy-to-dd-mm-yyyy) – Balasubramanian Sep 19 '17 at 08:54
  • Is `$scope.curdate` a date or a string? I suppose that it's a string so that's why it's only working with "new Date(..)". Then you've got a date object that can be tranformed to another format. – JeroenE Sep 19 '17 at 08:56

1 Answers1

4

Try this one

$scope.dateAsString = $filter('date')(new Date($scope.curdate), "yyyy/MM/dd");

Here is a working example

<!DOCTYPE html>
<html>
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.6.4/angular.min.js"></script>

<body>

    <div ng-app="myApp" ng-controller="datCtrl">

        <p>Date = {{ dateAsString }}</p>

    </div>

    <script>
        var app = angular.module('myApp', []);
        app.controller('datCtrl', function ($scope, $filter) {
            $scope.dateVal = '09/29/2017';
            $scope.dateAsString = $filter('date')(new Date($scope.dateVal), "yyyy/MM/dd");
        });
    </script>

    <p>The date filter formats a date object to a readable format.</p>

</body>

</html>
Nitheesh
  • 19,238
  • 3
  • 22
  • 49