一天的Moment JS间隔(以12小时格式) [英] Moment js interval for a day in 12 hours format

查看:155
本文介绍了一天的Moment JS间隔(以12小时格式)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在12小时格式中,我必须立即创建15分钟的间隔,并且可以30分钟的间隔正常工作.

In 12 hours format,i have to create a interval of 15 minutes in moment which is working fine with 30 minutes interval.

var hours = [];
    for (let hour = 0; hour < 24; hour++) {
      hours.push(moment({ hour }).format('h:mm a'));
      hours.push(
        moment({
          hour,
          minute: 30
        }).format('h:mm a')
      );
    }
  console.log( hours);

但是当工作15分钟时,显示格式错误.可以帮忙吗?

But when work with 15 minutes shows the wrong format.can anone help?

var hours = [];
    for (let hour = 0; hour < 24; hour++) {
      hours.push(moment({ hour }).format('h:mm a'));
      hours.push(
        moment({
          hour,
          minute: 15
        }).format('h:mm a')
      );
    }
  console.log( hours);

演示: http://jsfiddle.net/remus/rLjQx/

预期运营:12:00、12:15、12:30、12:45、1:00等

Expected Op: 12:00, 12:15,12:30,12:45,1:00 etc

推荐答案

每个循环只推入两个值.您需要每小时小时输入四个值.一种方法是在小时循环时间内循环分钟:

You are only pushing in two values per loop. You need to push in four values for every hour. One way would be to loop minutes within the hour loop hours:

var hours = [];
for (let hour = 0; hour < 24; hour++) {
  for (let minute = 0; minute < 60; minute += 15) {
    hours.push(moment({hour, minute }).format('h:mm a'));
  }
}

console.log(hours);

<script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.22.2/moment.js"></script>

这篇关于一天的Moment JS间隔(以12小时格式)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆