如何以小时和分钟为单位增加30分钟的时间 [英] How to add 30 minutes to time as hours and minutes
本文介绍了如何以小时和分钟为单位增加30分钟的时间的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一个 time
变量,它是f.ex 5:21
.我想增加30分钟,然后无论结果如何,我都想增加30分钟.我该如何在javascript中执行此操作?
即.我要间隔30分钟.
I Have a time
variable which is f.ex 5:21
. I want to add 30 minutes to it, then whatever result is, I want to add 30 minutes to it. How can I do this in javascript?
Ie. I want the interval of 30 min to the time I have.
var time = valgraph.datetime; // it's a dynamic time
var times = time.replace('Z', '');
var res = times.substr(11, 5); // time AS 5:20
推荐答案
尝试通过这种方式将分钟添加到您的时间字符串中.
Try this way to add minutes to your time string.
function addMinutesToTime(time, minsAdd) {
function z(n){
return (n<10? '0':'') + n;
};
var bits = time.split(':');
var mins = bits[0]*60 + +bits[1] + +minsAdd;
return z(mins%(24*60)/60 | 0) + ':' + z(mins%60);
}
result=addMinutesToTime('5:31',30);
alert(result);
请参见 FIDDLE
这篇关于如何以小时和分钟为单位增加30分钟的时间的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文