How to format the value of input[time] when bound to Date()-object

All we need is an easy explanation of the problem, so here it is.

I am binding a variable to an input-field of type time, but the displayed format is wrong.

It displays the time like this: 08:54:30,088
What I actually need is a format like this: 08:54.

I tried to set the value of the input field with a filter ( value={{ datetime.date | date : 'HH:mm' }} ) but my editor says the way I do this is wrong. Any ideas?

Here the compltete code:

HTML

 <input id="rep_time" class="form-control" type="time" ng-model="datetime.time" value={{ datetime.date | date : 'HH:mm' }}>

JS

 app.controller( 'newCtrl', function( $scope ) {  

     $scope.datetime = {
         date: new Date(),
         time: new Date()
       };
 } );

How to solve :

I know you bored from this bug, So we are here to help you! Take a deep breath and look at the explanation of your problem. We have many solutions to this problem, But we recommend you to use the first method because it is tested & true method that will 100% work for you.

Method 1

I circumvented the problem by defining the Date()-object a bit. It works but I don’t like this way of double defining.

HTML

 <input class="form-control" type="date" ng-model="datetime.date" placeholder="yyyy-MM-dd" min="2016-01-01" required />

 <input class="form-control" type="time" ng-model="datetime.time">

JS

$scope.datetime = {
        date: new Date(),
        time: ''
    };

$scope.datetime.time = new Date(
            $scope.datetime.date.getFullYear(),
            $scope.datetime.date.getMonth() + 1,
            $scope.datetime.date.getDate(),
            $scope.datetime.date.getHours(),
            $scope.datetime.date.getMinutes() );

UPDATE of js

with the idea of using a $filter from Jimbrooism I found a shorter way!

$scope.datetime = {
     date: new Date(),
     time: new Date( $filter( 'date' )( new Date(), 'yyyy-MM-dd HH:mm' ) )
   };

Method 2

Pls check out this

var app = angular.module("mainApp",  []);

app.controller('mainCtrl', function($scope, $filter){
  $scope.datetime = {
         date: new Date(),
         time: $filter('date')( new Date(), 'HH:mm')
       };

});
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.22/angular.min.js"></script>
<div ng-app="mainApp" ng-controller="mainCtrl">
  <input id="rep_time" class="form-control" type="time" ng-model="datetime.time" >
</div>

Method 3

Try this:

<input id="rep_time" class="form-control" type="time" ng-model="datetime.time" data-ng-value={{ datetime.date | date : 'shortTime'}}>

Method 4

Do you need the model to be bound to an input element?
Applying filters to input fields usually generates warnings or errors, since filters are one-way, and binding existing value to input and then changing them there is a two-way binding.

If you don’t need to be able to change the element, consider changing input to something else, like

<div ng-bind="datetime.date | date : 'HH:mm'"></div>

If you have to use input, Jimbroosim’s answer would work.

Note: Use and implement method 1 because this method fully tested our system.
Thank you 🙂

All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

Leave a Reply