Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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()
       };
 } );
like image 372
Sven Ya Avatar asked May 02 '16 07:05

Sven Ya


People also ask

How do you find the input date and time?

Definition and Usage The <input type="datetime-local"> defines a date picker. The resulting value includes the year, month, day, and time. Tip: Always add the <label> tag for best accessibility practices!

How do I change the input date format?

To set and get the input type date in dd-mm-yyyy format we will use <input> type attribute. The <input> type attribute is used to define a date picker or control field. In this attribute, you can set the range from which day-month-year to which day-month-year date can be selected from.


3 Answers

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' ) )
   };
like image 57
Sven Ya Avatar answered Sep 19 '22 19:09

Sven Ya


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>
like image 25
byteC0de Avatar answered Sep 17 '22 19:09

byteC0de


Try this:

<input id="rep_time" class="form-control" type="time" ng-model="datetime.time" data-ng-value={{ datetime.date | date : 'shortTime'}}>
like image 37
Diksha Tekriwal Avatar answered Sep 21 '22 19:09

Diksha Tekriwal