使用角度js在期望格式的两个日期时间之间的差异

时间:2017-12-26 10:46:24

标签: angularjs datetime

如何使用角度js检查所需格式的两个日期时间之间的差异。

await expect(await browser.element(by.cssContainingText('.common-form-error', 'We do not recognize that email address or password.Please try again or Sign-up to create an account.')))
            .toAppear(2000, 'Error message did not appear');

输出:

var ms = moment($scope.date1,"yyyy-MM-dd HH:mm:ss").diff(moment($scope.date2,"YYYY-MM-DD HH:mm:ss"));
            var d = moment.duration(ms);
            var s = Math.floor(d.asHours()) + moment.utc(ms).format(":mm:ss");
            console.log("Date diff.........",s);

Tried Code:

date1........ 2017-12-26 16:20:00
date2........ 2017-12-26 15:10:38
Diff- 01:10 
(If should ignore date because there is not date diffrence and if date diffrence is there then it show display like 03:10:05)

Diff (In format- DD-HH-MM)
DD-Days
HH-Hours
MM-Minutes

输出:

输入: 当前时间2017-12-29 10:19:41 Mydata- 2017-02-09 18:16:38

结果出现了问题 - {天:“23”,小时:“16”,分钟:“03”,秒:“03”}

2 个答案:

答案 0 :(得分:0)

您可以使用原生JavaScript实现它。



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

app.controller('MainCtrl', function($scope) {
  $scope.date1 = "2017-12-26 16:20:00";
  $scope.date2 = "2017-12-26 15:10:38"

  this.getDateDiff = function(date1, date2) {
    let d1 = new Date(date1);
    let d2 = new Date(date2);
    let diff = Math.abs(d1.getTime() - d2.getTime());
    let diffDate = new Date(0, 0, 0);
    diffDate.setMilliseconds(diff);
    let dayMills = ((60**2) * 24) * 1000;
    let days = Math.round(diff / dayMills);

    function formatNumberString(number) {
      return ('0' + number).slice(-2);
    }
    return {
      days: formatNumberString(days),
      hours: formatNumberString(diffDate.getHours()),
      minutes: formatNumberString(diffDate.getMinutes()),
      seconds: formatNumberString(diffDate.getSeconds())
    }
  }
  $scope.dateDiff = this.getDateDiff($scope.date1, $scope.date2);
});

<!DOCTYPE html>
<html ng-app="app">
  <head>
    <script data-require="angular.js@1.5.x" src="https://cdnjs.cloudflare.com/ajax/libs/angular.js/1.5.11/angular.min.js" data-semver="1.5.11"></script>
  </head>

  <body ng-controller="MainCtrl">
    <div>date1...... {{date1}}</div>
    <div>date2...... {{date2}}</div>
    <div>
      <span>Diff - </span>
      <span ng-if="dateDiff.days > 0">{{dateDiff.days}} : </span>
      <span>{{ dateDiff.hours }} : </span>
      <span>{{ dateDiff.minutes }}</span>
    </div>
  </body>

</html>
&#13;
&#13;
&#13;

答案 1 :(得分:-1)

看看这是否适合你。

您的控制器文件 -

$scope.date1 = "2017-12-26 16:20:00";
$scope.date2 = "2017-12-26 15:10:38";

var diff = moment.utc(date1, "yyyy-MM-DD HH:mm:ss").diff(moment.utc(date2, "yyyy-MM-DD HH:mm:ss"), 'minutes');
var days=diff/(24*60);
var hours=diff/60;
var minutes=diff%60;
$scope.arr = [];
if(parseInt(days)){
  arr.push(parseInt(days));
}
if(parseInt(hours)){
  arr.push(('0'+hours).substring(-1, 2));
}
if(minutes){
  arr.push(('0'+minutes).substring(-1, 2));
}
console.log(arr.join(':'));

你的html文件 -

...
<div>{{arr.join(':')}}</div>
...