Convert String to date () in angularjs

0

I am consuming from an array (json) the start and end dates. these are in string format. I would like to know how I convert those string to a date format yyy-mm-dd and print it on my controller with an alert. pdta All this I want to do to then get dates that are on those dates given (start and end)

var myApp = angular.module('myApp',[]);

//myApp.directive('myDirective', function() {});
//myApp.factory('myService', function() {});

function MyCtrl($scope) {
    $scope.fechas = [
  {"fechaInicio": "01-04-2016"},{"fechaFin": "05-08-2016"},
  ];

  for(i=0;i<$scope.fechas.length;i++){
    alert($scope.fechas[i].fechaInicio);//esto quiero convertir a formato fecha yyy-mm-dd
    alert($scope.fechas[i].fechaFin); //esto quiero convertir a formato fecha yyy-mm-dd
  }
}

Thanks in advance.

    
asked by Dimoreno 21.05.2016 в 08:00
source

2 answers

0

You can use the angular date filter or a manipulation of dates library like moment.js

Using the date filter you can pause your date both in the view and in the controller but this has the disadvantage that this filter expects your entry date to come as type Date or as a string in the format ISO 8601 .

Your dates come in a dd-MM-yyyy format, which is not a standard so you can use string manipulation or regular expressions to achieve the desired effect as long as the input format does not change. Here is an example.

angular.module('app', [])
  .controller('MyCtrl', function($scope, $filter) {
    var fechaInicio, fechaFin;

    $scope.fechas = [{
      "fechaInicio": "01-04-2016"
    }, {
      "fechaFin": "05-08-2016"
    }];

    var parseDate = $filter('parseDate');

    for (i = 0; i < $scope.fechas.length; i++) {
      fechaInicio = parseDate($scope.fechas[i].fechaInicio);
      fechaFin = parseDate($scope.fechas[i].fechaFin);
      alert(fechaInicio);
      alert(fechaFin);

    }
  })
  .filter('parseDate', function() {
    return function(input) {
      var result = input;
      var format = /^(\d{2})-(\d{2})-(\d{4})$/;
      var match = format.exec(input);
      if (input && match) {
        result = match[3] + '-' + match[2] + '-' + match[1];
      }
      return result;
    }
  });
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>
<div ng-app="app" ng-controller="MyCtrl">
  <ul ng-repeat="fecha in fechas">
    <li>Fecha inicio: {{fecha.fechaInicio | parseDate }}</li>
    <li>Fecha fin: {{fecha.fechaFin | parseDate }}</li>
  </ul>
</div>

Here I leave another using moment.js which in my opinion is more complete since you can easily modify it in the future to any other input format.

angular.module('app', [])
  .controller('MyCtrl', function($scope, $filter) {
    var fechaInicio, fechaFin;

    $scope.fechas = [{
      "fechaInicio": "01-04-2016"
    }, {
      "fechaFin": "05-08-2016"
    }];

    for (i = 0; i < $scope.fechas.length; i++) {
      fechaInicio = moment($scope.fechas[i].fechaInicio, 'DD-MM-YYYY').format('YYYY-MM-DD');
      fechaFin = moment($scope.fechas[i].fechaFin, 'DD-MM-YYYY').format('YYYY-MM-DD');
      alert(fechaInicio);
      alert(fechaFin);

    }
  })
  .filter('parseDate', function() {
    // Lo mismo en un filtro
    return function(input) {
      var result = input;
      if (input) {
        result = moment(input, 'DD-MM-YYYY').format('YYYY-MM-DD');
      }
      return result;
    }
  });
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>
<script src="http://momentjs.com/downloads/moment.min.js"></script>
<div ng-app="app" ng-controller="MyCtrl">
  <ul ng-repeat="fecha in fechas">
    <li>Fecha inicio: {{fecha.fechaInicio | parseDate }}</li>
    <li>Fecha fin: {{fecha.fechaFin | parseDate }}</li>
  </ul>
</div>
    
answered by 23.05.2016 / 16:33
source
0

You can simply use javascript

//Ej.    

$scope.date1 = new Date($scope.fechas[0].fechaInicio);

Since then you have a Date object as a variable, and you can use the available javascript methods

//Ej. 
alert($scope.date1.getFullYear()+"-"+($scope.date1.getMonth()+1)+"-"+$scope.date1.getDate());

[ link

    
answered by 21.05.2016 в 22:33