【发布时间】:2016-12-05 09:42:29
【问题描述】:
在 AngularJS 1.5 中,我有一个带有“时间”类型的输入字段的表单。它可以工作,但显示的时间应该是 HH:mm - 没有秒等。
Chrome 默认执行此操作,但 Firefox 和 Internet Explorer 以秒和毫秒显示(例如,“14:56:00.000”而不是“14:56”)。
如何在所有浏览器中显示所需的时间格式?
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Example - example-time-input-directive-production</title>
<script src="//code.angularjs.org/snapshot/angular.min.js"></script>
</head>
<body ng-app="timeExample">
<script>
angular.module('timeExample', [])
.controller('DateController', ['$scope', function($scope) {
$scope.example = {
value: new Date(1970, 0, 1, 14, 56)
};
}]);
</script>
<form name="myForm" ng-controller="DateController as dateCtrl">
<label for="exampleInput">Pick a time between 8am and 5pm:</label>
<input type="time" id="exampleInput" name="input" ng-model="example.value"
placeholder="HH:mm" min="08:00" max="17:00" required />
<div role="alert">
<span class="error" ng-show="myForm.input.$error.required">
Required!</span>
<span class="error" ng-show="myForm.input.$error.time">
Not a valid date!</span>
</div>
<tt>value = {{example.value | date: "HH:mm:ss"}}</tt><br/>
<tt>myForm.input.$valid = {{myForm.input.$valid}}</tt><br/>
<tt>myForm.input.$error = {{myForm.input.$error}}</tt><br/>
<tt>myForm.$valid = {{myForm.$valid}}</tt><br/>
<tt>myForm.$error.required = {{!!myForm.$error.required}}</tt><br/>
</form>
</body>
</html>
谢谢!
【问题讨论】:
-
你是对的。但我的意思是输入字段中的日期格式,而不是文本字段中的日期格式。很抱歉造成误解。
-
添加了一些可能有用的链接