【问题标题】:i want compare the date and time and want show the differnce time in angularjs我想比较日期和时间,并想在 angularjs 中显示不同的时间
【发布时间】:2015-09-08 13:35:04
【问题描述】:

我想比较日期和时间,并想在 angularjs 中显示差异时间,我是 yyyy/MM/dd 格式的 gettig 日期,

【问题讨论】:

  • 酷,我想建立一个新的 Facebook,请。
  • 请贴出你到目前为止尝试过的代码和你得到的结果,以及你想要达到的目标。

标签: javascript angularjs


【解决方案1】:

除了由于缺少自己的代码而导致您的问题通常不值得回答这一事实之外,该问题已在 SO 上多次回答。下一次,您应该添加更多关于您的问题和您正在努力解决的代码的信息。

无论如何,我有一个简单的解决方案给你(由@Tni 提供支持):

var myApp = angular.module('myApp', []);

myApp.controller('MyCtrl', function($scope) {
    $scope.date1 = '2015/01/01';
    $scope.date2 = '2015/12/31';
    
    $scope.getDateDiff = function() {
    	var date1 = new Date($scope.date1),
            date2 = new Date($scope.date2),
            timeDiff = Math.abs(date2.getTime() - date1.getTime());
        $scope.dateDiff = 'Amount of days difference: ' + Math.ceil(timeDiff / (1000 * 3600 * 24)); 
    }
});
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>

<div ng-app="myApp" ng-controller="MyCtrl">
    Date 1: <input type="text" ng-model="date1">
    <br /><br />
    Date 2: <input type="text" ng-model="date2">
    <br /><br />
    <button ng-click="getDateDiff()">Get Date Diff!</button>
    <br /><br />
    {{dateDiff}}
</div>

【讨论】:

    【解决方案2】:

    请检查此链接,您将获得更好的解决方案

    JSFiddle Demo

    html:

    <div ng-app ng-controller="Ctrl"> 
       {{date | date:'dd-MM-yyyy hh:mm '}} <br>
    </div>
    

    js:

    function Ctrl($scope)
    {
        $scope.date = new Date(); 
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2010-10-07
      • 1970-01-01
      • 2017-01-16
      • 2021-07-13
      • 1970-01-01
      相关资源
      最近更新 更多