使用moment.js创建过去24小时的数组

时间:2018-05-07 06:26:00

标签: javascript arrays datetime momentjs

我正在尝试创建过去24小时的数组,其值仅为Moment.js中AM / PM(不是分钟,秒,甚至日历日期)的小时数。

它现在有效,但是当我只需要几个小时的时间时,它会在每个元素中提供整个日期数据(Sun May 06 2018 201:24:29 GMT-0700):

JS代码:

var hoursPerDay = 24;
var time = [];

function timeOneDay() {
  var formattedTime;
  for (i = 0; i < hoursPerDay + 1; i++) { //fill in all of the hours
    formattedTime = (moment().subtract(i, "hours"));
    formattedTime.format("hA"); //give the time in format X AM/PM
    time.unshift(formattedTime); //add to beginning of array
  } //do this for all 24 hours
}
timeOneDay();
console.log(time);
<script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.22.1/moment.min.js"></script>

1 个答案:

答案 0 :(得分:2)

var hoursPerDay = 24; var time = []; function timeOneDay(){ var formattedTime; for(i =0; i < hoursPerDay+1 ; i++){ //fill in all of the hours formattedTime = (moment().subtract(i, "hours")).format("hA"); //give the time in format X AM/PM time.unshift(formattedTime); //add to beginning of array } //do this for all 24 hours } timeOneDay(); console.log(time);返回格式化的值。您没有将其存储在变量

<script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.22.1/moment.min.js"></script>
Map<String, Object> BackspaceKeyEvent  = new HashMap<String, Object>();
BackspaceKeyEvent.put("key", "8");
getDriver().executeScript("mobile:key:event", BackspaceKeyEvent);

相关问题