javascript中的时间数组Ember JS

时间:2014-04-25 00:08:29

标签: javascript arrays time ember.js

我想用以下格式创建一个系统时间数组

["4:01pm","4:06pm","4:11pm","4:16pm","4:21pm","4:26pm"]

假设系统当前时间为4:26pm

如何通过获取当前系统时间在javascript中创建此内容

1 个答案:

答案 0 :(得分:1)

var i = 6, result = [], time = new Date();
while (i--) {
  var hours = time.getHours(), minutes = time.getMinutes();
  result[i] = ((hours - 1) % 12 + 1) + ':' +
      (minutes < 10 ? '0' : '') +
      minutes + (hours < 12 ? 'am' : 'pm');
  time = new Date(time - 5 * 60 * 1000);
}
console.log(result);
// ["8:59am", "9:04am", "9:09am", "9:14am", "9:19am", "9:24am"]