我想使用angularJs将datetime-local的默认值设置为当前日期和时间(时间应仅包含小时和分钟,如' 2014-01-02T11:42'。)。
修改: 以下是我的HTML代码
<center><input type="datetime-local" name="start date" ng-model="endDate" id="from_date"></center>
angularJs代码
$scope.endtDate=new Date(new Date().getTime()).toLocaleString();
通过这种方法,我可以设置当前的日期和时间,但它也包含秒和毫秒。
答案 0 :(得分:1)
更改了代码并解决了我的问题
$scope.endDateTime=new Date();
var h = $scope.endDateTime.getHours();
var m = $scope.endDateTime.getMinutes();
$scope.endDateTime.setHours(h,m,0,0);
这只会设置小时和分钟。 干杯:)
答案 1 :(得分:0)
您应该使用日期过滤器来显示日期..
请看以下链接..
http://www.w3schools.com/angular/tryit.asp?filename=try_ng_ref_date
https://docs.angularjs.org/api/ng/filter/date
我希望这会有所帮助
答案 2 :(得分:0)
使用角度过滤器放置日期变量而不是数字。
<script>
var app = angular.module('myApp', []);
app.controller('dateCtrl', function($scope) {
$scope.today = new Date();
});
</script>
Plunker:https://plnkr.co/edit/HXDsILCtyliiamxdABZn?p=preview
<body ng-app="" ng-controller="dateCtrl">
<span>{{today | date:"MM/dd/yyyy'T' h:mm"}}</span><br>
</body>