Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/461.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/date/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript,返回加上5分钟的小时和分钟_Javascript_Date_Datetime_Math_Time - Fatal编程技术网

Javascript,返回加上5分钟的小时和分钟

Javascript,返回加上5分钟的小时和分钟,javascript,date,datetime,math,time,Javascript,Date,Datetime,Math,Time,我有一个简单的小时和分钟数组: var hours = ['01', '02', '03', '04', '05', '06', '07', '08', '09', '10', '11', '12']; var minutes = ['00', '05', '10', '15', '20', '25', '30', '35', '40', '45', '50', '55']; 如您所见,分钟数加上5分钟 现在我想得到距离现在日期时间最近的未来小时和分钟 例如,如果现在的日期时间是'09:07'

我有一个简单的小时和分钟数组:

var hours = ['01', '02', '03', '04', '05', '06', '07', '08', '09', '10', '11', '12'];
var minutes = ['00', '05', '10', '15', '20', '25', '30', '35', '40', '45', '50', '55'];
如您所见,分钟数加上5分钟

现在我想得到距离现在日期时间最近的未来小时和分钟

例如,如果现在的日期时间是'09:07',我想从 数组“09:10”或如果现在的日期时间是“10:21”,则返回“10:25”, 因为是阵列中可用的未来最近日期

我当然想知道它们(小时、分钟)都对应于哪个数组索引


如何实现这一点?

如果您能够获得当前分钟值,那么您应该能够在分钟数组中使用它

var minutes = ['00', '05', '10', '15', '20', '25', '30', '35', '40', '45', '50', '55', '00'];
var minuteIndex = Math.ceil(curMinutes / 5);
console.log(minutes[minuteIndex]);
请注意,我在数组的末尾添加了一个额外的
'00'
。这样,对于大于
55
的任何值,都不需要边界条件。这也适用于精确的分钟值(如12:00)

如果您想让小时工作,那么您只需满足分钟数为55的情况(否则,小时数只是其当前值)


请注意,我还在开始时添加了一个额外的
12
,以简化条件。

你可以做简单的数学运算,得到你得到的“5分钟”数,然后得到最接近的最高整数

 var date = new Date;

 var indexMinutes = Math.ceil(date.getMinutes() / 5);
 var indexHours = 0;

 if (indexMinutes == 12){
     indexMinutes = 0;
     indexHours = 1
 }

 indexHours = indexHours + (date.getHours() % 12 - 1);

 if (indexHours < 0 ) indexHours = 11;

 var paddedHour = hours[indexHours] + ":"+ minutes[indexMinutes];
var日期=新日期;
var indexMinutes=Math.ceil(date.getMinutes()/5);
var指数小时=0;
如果(indexMinutes==12){
指数分钟=0;
索引小时=1
}
indexHours=indexHours+(date.getHours()%12-1);
如果(indexHours<0)indexHours=11;
var paddedHour=hours[indexHours]+“:”+分钟[indexMinutes];

我想你可以不用数组
功能板(num){
返回数值<10?'0'+num:num;
}
函数格式化时间(小时、分钟){
var roundMin=数学单元(分钟/5)*5;
var roundHour=小时;
如果(roundMin==60){
roundMin=0;
整小时=(小时数+1)%12;
}
返回pad(圆小时)+“:”+pad(圆分钟);
}
console.log(格式化时间(12,59));//formattime(date.getHour(),date.getMinutes());
日志(格式化时间(11,31));
变量日期=新日期();
log(formattime(date.getHours()%12,date.getMinutes())

对索引进行一些调整的解决方案

var小时数=['01','02','03','04','05','06','07','08','09','10','11','12'],
分钟=['00','05','10','15','20','25','30','35','40','45','50','55'],
日期=新日期(),
indexHour=date.getHours(),
indexMinute=Math.ceil(date.getMinutes()/5);
如果(索引分钟>=12){
indexHour++;
指数分钟%=12;
}
指数小时=(指数小时+11)%12;

写(小时[索引小时]+':'+分钟[索引分钟])类似于
Math.ceil(currentMins/5)
?@Hacketo看起来很酷。。。。需要尝试一下,我会喜欢的答案,这样我就可以接受他们!似乎不起作用,如果它是23,它返回5,我需要返回25在这种情况下我不理解你的意思,什么的索引D他们的意思是
minutes[Math.ceil(currentMins/5)]
您需要检查最终索引
00
,例如56将为您提供索引12(错误).我认为它在
12:59
情况下不能正常工作@Hacketo@Hacketo我想现在它将在
12:00
OP询问数组索引可能他还没有看到其他方法,答案在这里,以便他可以从多个答案/方法中进行选择
 var date = new Date;

 var indexMinutes = Math.ceil(date.getMinutes() / 5);
 var indexHours = 0;

 if (indexMinutes == 12){
     indexMinutes = 0;
     indexHours = 1
 }

 indexHours = indexHours + (date.getHours() % 12 - 1);

 if (indexHours < 0 ) indexHours = 11;

 var paddedHour = hours[indexHours] + ":"+ minutes[indexMinutes];
var oDate = new Date();
if (oDate.getMinutes() % 5 != 0) {
  oDate.setMinutes(((oDate.getMinutes()) / 5 + 1) * 5);
}
return oDate;