在角度js中 - 如何获得按钮点击的当前时间

时间:2017-04-17 06:16:22

标签: javascript angularjs time

我有两个按钮,如进出,我需要显示当前时间..一旦点击我需要发送当前时间和日期的任何按钮

 <label>Today's Date : {{currentdate}}</label>
  <label>Current Time :  {{currenttime}}</label>

  <button type="submit" class="btn" ng-disabled="disabledin"  value="in" ng-
  click="submitFun($event)">IN</button>
  <button type="button" class="btn" ng-disabled="disabledout" value="out" 
   ng-click="submitFun($event)">OUT</button>

4 个答案:

答案 0 :(得分:1)

你正在寻找这样的东西:

angular.module("myApp", [])
  .controller("myCtrl", function($scope) {
    $scope.someFunction = function() {
      $scope.date = new Date();
    }
  })
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>

<body ng-app="myApp" ng-controller="myCtrl">
  <pre>current date: {{date | date: 'dd/MM/yyyy'}}</pre>
  <pre>time: {{date | date: 'HH:mm:ss'}}</pre>
  <button ng-click="someFunction()">show date time</button>
</body>

答案 1 :(得分:0)

var time = new Date()

答案 2 :(得分:0)

在您的控制器中,您可以使用:

$scope.submitFun($event){
    $scope.currentDate = new Date();
    $scope.currentTime = new Date().getTime();
}

您可以在html中使用过滤器更改日期格式:

<label>Today's Date : {{currentdate | date:'yyyy-MM-dd HH:mm:ss Z'}}</label>

阅读一些DOC

答案 3 :(得分:0)

(function () {
    function checkTime(i) {
        return (i < 10) ? "0" + i : i;
    }

    function startTime() {
        var today = new Date(),
        $scope.currentdate = today;
            h = checkTime(today.getHours()),
            m = checkTime(today.getMinutes()),
            s = checkTime(today.getSeconds());
        $scope.currenttime = h + ":" + m + ":" + s;
        t = setTimeout(function () {
            startTime()
        }, 500);
    }
    startTime();
})();