1

Is there any way for Angular to take the string from the date/time inputs? Currently, I have this input, for example:

<input class="admin-input" type="time" ng-model="newEvent.endHour"/>

I'd like to show the value in model as HH:mm but, when I print its value, it shows the full date plus the full time. Is there anyway to get just the string shown in the input (HH:mm format) into the ng-model?

Thanks in advance.

2

1 Answer 1

2

You can use the angular date filter,

<input class="admin-input" type="time" ng-model="newEvent.endHour | date:'HH:mm'"/>

EDIT If you want to bind it to a model then add a watch and filter it inside the controller,

DEMO

var app = angular.module('App', []);
app.controller('myctrl', function($scope,$filter) {
  $scope.endHour = new Date();
  $scope.$watch('endHour', function(newValue) {
    $scope.endHour = $filter('date')(newValue, 'HH:mm');
  });
});
<!DOCTYPE html>
<html ng-app="App">

<head>
  <script data-require="angular.js@*" data-semver="1.2.14" src="//code.angularjs.org/1.2.14/angular.js"></script>
  <link rel="stylesheet" href="style.css" />
  <script src="script.js"></script>
</head>

<div ng-app="App" ng-controller="myctrl">
  <input type="datetime" ng-model="endHour" class="form-control" />
</div>
</body>

</html>

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

5 Comments

It seems you can't assign a filter to the ng-model, it doesn't work
i would suggest you to do it on the controller unless you are displaying it in expression
Still the same, it seems like it will always send the full date in both time and dates: 2017-03-13T23:00:00.000Z (also timezone is incorrect)
did you check the demo
Yes, I did the same, which also is like this one stackoverflow.com/questions/36976716/…

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.