Angularjs 在 10 分钟内将最后一小时拆分为切片



有没有办法我可以把现在到最后一个小时之间的时间分成 10 分钟的切片?如下所示

10:00:00
09:50:00
09:40:00
09:30:00
09:20:00
09:10:00
09:00:00

开始时间代码为

//time now -1 hour
$scope.startTime = new Date( (new Date) * 1 - 1000 * 3600 * 1 );

对于我正在使用的末日

//time now
$scope.endTime = new Date();

为了使其成为真正的日期,我正在使用此功能

Date.prototype.yyyymmddhhmm = function() {
       var yyyy = this.getFullYear();
       var mm = this.getMonth() < 9 ? "0" + (this.getMonth() + 1) : (this.getMonth() + 1); // getMonth() is zero-based
       var dd  = this.getDate() < 10 ? "0" + this.getDate() : this.getDate();
       var hh = this.getHours() < 10 ? "0" + this.getHours() : this.getHours();
       var min = this.getMinutes() < 10 ? "0" + this.getMinutes() : this.getMinutes();
       var sec = this.getSeconds() < 10 ? "0" + this.getSeconds() : this.getSeconds();
       return "".concat(yyyy + '-').concat(mm + '-').concat(dd + ' ').concat(hh + ':').concat(min + ':').concat(sec);
    };

我需要 10 分钟切片的是折线图标签。

谢谢

我已经修改了代码,如下所示,

JS代码如下

var myApp = angular.module('myApp',[])
.controller('MyCtrl', function($scope){
   $scope.timeList = [];
   $scope.seconds = 0;
   var time = formatAMPM(new Date( (new Date) * 1 + 1000 * 0 * 1 ));
   $scope.timeList.push(time);
   for(i=0;i<=5;i++)
   {
      $scope.seconds = $scope.seconds + 600;
      var time = formatAMPM(new Date( (new Date) * 1 + 1000 * $scope.seconds * 1 ));
      $scope.timeList.push(time);
   }
   function formatAMPM(date) {
    var hours = date.getHours();
    var minutes = date.getMinutes();
    var ampm = hours >= 12 ? 'pm' : 'am';
    hours = hours % 12;
    hours = hours ? hours : 12; // the hour '0' should be '12'
    minutes = minutes < 10 ? '0'+minutes : minutes;
    var strTime = hours + ':' + minutes + ' ' + ampm;
    return strTime;
    }
});

HTML代码如下,

<div ng-controller="MyCtrl">
  <div ng-repeat="time in timeList">
    {{time}}
  </div>
</div>

检查此 JSFiddle 链接:演示

输出如下:

3:44 下午3:54 下午4:04 下午4:14 下午4:24 下午4:34 下午4:44 下午

希望这对你有帮助

谢谢

var endTime = new Date();
var startTime = new Date( endTime.getTime() - 1000 * 3600 );
var time = startTime;
while (time < endTime) {
    time = new Date(time.getTime() + 1000 * 60 * 10);
    // do something with the generated time
}

这不会使用您尚未使用的任何内容,我对您卡住的内容感到困惑。

最新更新