How to add 30 minutes to time as hours and minutes

落花浮王杯 提交于 2021-02-04 08:41:27

问题


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

回答1:


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

SEE FIDDLE




回答2:


try this...

 // dateTime is the variable having your own date
     var d = new Date(dateTime);
     d.setMinutes(d.getMinutes() + 30);


来源:https://stackoverflow.com/questions/28912116/how-to-add-30-minutes-to-time-as-hours-and-minutes

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!