问题
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