krishna mohan
krishna mohan

Reputation: 49

date format is not changing in angular js want to get in dd/mm/yyyy format in angular js

i have a date time data "11/12/2019 12:00:00 AM" , that i want to convert into 12/11/2019 in dd/mm/yyyy format but unable to change it.

<p>
 {{data.DeviceTimeStamp |  date : "dd.MM.y"}}
</p>

Upvotes: 0

Views: 153

Answers (3)

krishna mohan
krishna mohan

Reputation: 49

created custom filter

app.filter('parseDate', function () {
    return function (input) {
        return new Date(input);
    };
});

then applied custom filter in my binding

                <p>
                    {{ data.DeviceTimeStamp | parseDate | date:'d/M/yyyy' }}
                </p>

Upvotes: 0

Nikhil Mohanan
Nikhil Mohanan

Reputation: 1260

    angular.module('myApp', [])
    .controller("myCtrl", ["$scope", function(s) {
      s.date = new Date('11/12/2019 12:00:00 AM');
    }]);
 <script src="https://cdnjs.cloudflare.com/ajax/libs/angular.js/1.2.12/angular.min.js">        </script>
    <div ng-app="myApp">
       <div ng-controller="myCtrl">
           <div class="col-md-12">{{date | date:'dd/MM/yyyy'}}</div>
      </div>
    </div>

You can try like above.Here new Date() function parses your date string to a date object where it needs to be a valid input for date filter. Hope it helps.

Upvotes: 0

Ayyub Kolsawala
Ayyub Kolsawala

Reputation: 849

angular.module('myApp', [])
.controller("example", ["$scope", function(s) {
  s.date = new Date();
}])
<script src="https://cdnjs.cloudflare.com/ajax/libs/angular.js/1.2.12/angular.min.js"></script>
<div ng-app="myApp">
   <div class="container" ng-controller="example">
      <div class="row">
         <hr />
         <div class="col-md-12">You landed here on {{date | date:'dd/MM/yyyy'}}, and I am in controller <code>example</code></div>
      </div>
   </div>
</div>

You can specify the array to contain the date inside an object and you can use this.

Upvotes: 1

Related Questions