Loading date in millisecond format in angularjs

1

I need to load%% of dates that are stored in values in millisecond format.

Example

<label>Data de expiração:</label>
<input  class="form-control" type="date" name="dataexpiracao" ng-model="PontoRelevancia.dados_Evento.dataexpiracao | date:'dd/MM/yyyy' "> 

When I use this filter using input type="date" it can put in the desired format, but using input type="text" it does not work .

What can I do to load date information in millisecond using input type="date" ?

    
asked by anonymous 07.08.2017 / 20:35

1 answer

1

In this case, you need to implement a policy that preserves the original format, while converting the value to a datatype that can be used with input type='datetime-local' .

Functional example below:

angular.module('myApp', [])
.directive('epochDatetime', function () {
  return {
    restrict: 'A',
    require: 'ngModel',
    link: function (scope, element, attrs, ngModel) {
      ngModel.$formatters.push(function (value) {
        // transforma o modelo para um tipo Date().
        return new Date(value);
      });
     
      ngModel.$parsers.push(function(value) {
        // converte o valor Date do modelo para milissegundos pós-epoch.
        return value.getTime();
      });
    }
  };
})
.controller('myController', function($scope){
  $scope.dataReferencia = 1496268000000;
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/angular.js/1.5.8/angular.js"></script><divng-app="myApp">
  <div ng-controller='myController'>
    <input type="datetime-local" ng-model="dataReferencia" epoch-datetime id="exampleInput"/>
    <pre>{{dataReferencia}}</pre>
  </div>
</div>
    
07.08.2017 / 21:12