Angularjs Format date

Asked

Viewed 1,541 times

0

As a data format in the input type controller="DATE"?

i have this return in controller...

Sat Aug 06 2016 00:00:00 GMT-0300 (Hora oficial do Brasil)

Mon Aug 01 2016 00:00:00 GMT-0300 (Hora oficial do Brasil)

has how to format to

01-08-2016?

3 answers

5

You can use the date filter angular.

Editing to change the Data format in the Controller

Example:

var app = angular.module('myApp', []);
app.controller('datCtrl', function($scope, $filter) {
    $scope.today = new Date();
    $scope.todayString = $filter('date')(new Date(), 'dd-MM-yyyy');
  
});
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>


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

<p>Data = {{ today | date:'dd-MM-yyyy' }}</p>
  
<p>Data direto do Controller = {{ todayString }}</p>

</div>

  • I didn’t even remember this rsrs. + 1. You can put the js function, $filter? just to complete.

  • It did not solve the problem, my problem is in the controller, I need to take this information in the controller, not in the view... I want to use the input as a parameter to make a request and in case the format of the next date is not in the format it should be... in which case it would be 01-08-2016

  • I edited, you can use the angular date filter, both in JS and HTML bind.

1

0

If it is inside the Angular controller, then one should create a formatter using Javascript only.

Here is a list of references to date functions for Javascript.

function pad(n) {
    return n < 10 ? '0' + n : n;
}

function formataMinhaData (data) {
    return pad(data.getDate()) + '/' + pad(data.getMonth() + 1) + '/' + data.getFullYear();
}

Browser other questions tagged

You are not signed in. Login or sign up in order to post.