1

I have html input:

 <input type="time" id="exampleInput" name="input" ng-model="ctrl.time"
               placeholder="HH:mm:ss" min="00:00:00" max="24:00:00" required />

From my input, I'm getting something like this into my 'ctrl.time':

"1970-01-01T02:04:00.000Z"

but I expect only time like: 14:03:00

Is there a solution to make the data being formatted before it goes to Angular variable or to format it inside ?

Thank You in advance for your answer.

3
  • 3
    Why not just handle the variable on the controller? Seems like the cleaner approach Commented Apr 8, 2016 at 14:18
  • 1
    I would advise you to look at moment.js for ensuring that your time input is giving you what you want, and allow to account for time-zones and other locale specific issues. Commented Apr 8, 2016 at 14:24
  • ng-moment or use a filter, or bind a factory to your view model. you could do it in the controller but doing business logic in the controller isn't clean at all. Commented Apr 8, 2016 at 15:19

1 Answer 1

2

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

app.controller('main', function($scope) {
  $scope.ctrl = {};
  $scope.show = function(){
    console.log( $scope.ctrl.time);
    console.log(moment($scope.ctrl.time).format('HH:mm:ss'));
  };
});
<html>
  <head>
    <script data-require="moment.js@*" data-semver="2.10.2" src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.10.2/moment.min.js"></script>    
    <script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.5.0/angular.min.js"></script>    
  </head>

  <body ng-app="plunker" ng-controller="main">
    <input type="time" id="exampleInput" name="input" 
          ng-model="ctrl.time" 
          ng-change="show()" 
          placeholder="HH:mm:ss" min="00:00:00" max="24:00:00" required="" />
    
  </body>
</html>

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.