简体   繁体   English

12小时格式的一天的瞬间js间隔

[英]Moment js interval for a day in 12 hours format

In 12 hours format,i have to create a interval of 15 minutes in moment which is working fine with 30 minutes interval. 在12小时格式中,我必须创建一个15分钟的间隔,这是间隔30分钟工作正常。

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

But when work with 15 minutes shows the wrong format.can anone help? 但是,当15分钟的工作显示错误的格式时,一个人有帮助吗?

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

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

Expected Op: 12:00, 12:15,12:30,12:45,1:00 etc 预期操作:12:00,12:15,12:30,12:45,1:00等

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> 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM