angularjs - 如何从 angularjs input-time 获取小时和分钟
问题描述
我知道我必须使用<input type="time".....>
让用户输入时间。但我迫切需要知道如何单独使用小时和分钟的值进行进一步处理。我只是到处寻找关于如何接受和打印整个时间的例子(比如object.value)。但我目前需要专门为我手头的任务提取小时和分钟。这是迄今为止我发现的唯一类型的代码。
<!doctype html>
<html lang="en">
<head>
<title>AngularJS Directives : input[time]</title>
<script src="angular.js"></script>
<style>
b{font-family:Papyrus; color:#fa4b2a; font-size: 20px;}
</style>
</head>
<body ng-app="timeDemo">
<script>
angular.module('timeDemo', [])
.controller('timeController', ['$scope', function($scope) {
$scope.sample = {
value: new Date(1999, 0, 1, 15, 30, 0)
};
}]);
</script>
<form name="demoForm" ng-controller="timeController as timeCtrl">
<label for="sampleInput">Select a time from 6 am to 6 pm</label>
<input type="time" id="sampleInput" name="input" ng-model="sample.value"
placeholder="HH:mm:ss" min="06:00:00" max="18:00:00" required />
<!-- min 6 am and max 6 pm i.e 18:00 -->
<div role="alert">
<span class="error" ng-show="demoForm.input.$error.required">
Input is Required!</span>
<!-- Required Error -->
<span class="error" ng-show="demoForm.input.$error.time">
Input Date is not Valid!</span>
<!-- Validation Error -->
</div>
<i>value = <b>{{sample.value | date: "HH:mm:ss"}}</b></i><br/>
<i>demoForm.input.$valid = <b>{{demoForm.input.$valid}}</b></i><br/>
<i>demoForm.input.$error = <b>{{demoForm.input.$error}}</b></i><br/>
<i>demoForm.$valid = <b>{{demoForm.$valid}}</b></i><br/>
<i>demoForm.$error.required = <b>{{!!demoForm.$error.required}}</b></i><br/>
</form>
</body>
</html>
解决方案
Date 对象应该返回您要查找的内容...只需根据您的输入创建一个新的 Date 对象并使用该对象的方法。例如:
var d = new Date(sample.value);
var hours = d.getHours();
var minutes = d.getMinutes()
有关详细信息,请参阅https://www.w3schools.com/jsref/jsref_obj_date.asp
推荐阅读
- html - 我有一个网页。当用户尝试查看网页的源代码时,我想显示空白页。我该怎么做?
- spring-boot - Spring Jpa Query by Example 集合
- javascript - For in 循环遍历数组而不是 JS 函数
- java - 试图在 2 个单独的数组列表中链接对象
- javascript - 查找错误:根据页面滚动位置突出显示侧边菜单
- sql - 如何根据临时表 SQL Server 2008 R2 从一个表中提取数据?
- node.js - Node.JS EXE 删除执行文件
- javascript - 如何在单个页面上显示多个幻灯片
- c - ADC中断没有被调用
- python - 从没有表元素的 html 创建一个 csv 文件以与 BeautifulSoup 一起使用