如何从html输入类型=“”中只获得Angular控制器变量中的时间值;时间”;

How to get only time value in Angular controller variable from html input type="time"

本文关键字:时间 变量 控制器 Angular 类型 html 输入      更新时间:2024-03-23

我有html输入:

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

根据我的输入,我在我的"ctrl.time"中得到了这样的东西:

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

但我预计只有像14:03:00 这样的时间

有没有一种解决方案可以在数据进入Angular变量之前进行格式化,或者在内部进行格式化?

提前感谢您的回答。

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>