Angularjs在10分钟切片中最后一小时分开

时间:2016-01-22 09:16:14

标签: angularjs charts

有没有办法可以在现在和最后一小时之间花时间在AngularJS中将它分成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分钟切片用于折线图标签。

由于

2 个答案:

答案 0 :(得分:2)

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

js代码如下

{'C01234':'pass' , 'pass'}

{'C2456' : 'fail' , 'pass', 'pass'}

{'C53675': 'pass' , 'pass'}

Html代码如下,

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;
    }
});

检查此JSFiddle链接:Demo

OutPut如下:

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

希望这有助于你

由于

答案 1 :(得分:1)

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
}

这不会使用你还没有使用的任何东西,我对你所坚持的内容感到困惑。

相关问题