[英]How do I make sure my function works for my variable?
function numberToTime(num){
var d = 0, h = 0, m = 0;
var numToMinutes = num*60;
while(numToMinutes > 59){
numToMinutes -= 60;
h++;
if(h > 23){
h-= 24;
d++;
}
m = numToMinutes;
}
if( d > 0){
return d + " days " + h + " hours " + m +" minutes ";
}else{
return h+":"+m;
}
This code was given to me by a very nice user here on Stack Overflow. 这段代码是由Stack Overflow上的一个非常好的用户提供给我的。 Since I am very new to programming, especially JavaScript I have no idea where to put my variable. 因为我对编程非常陌生,尤其是JavaScript,所以我不知道将变量放在何处。 I have a var howLong = (0,1 * amount + 0,2 * time)
I want to convert it to hours and minutes with the code above, but I don't know how to tell the function it's about var howLong
. 我有一个var howLong = (0,1 * amount + 0,2 * time)
我想用上面的代码将其转换为小时和分钟,但是我不知道如何告诉函数它与var howLong
。
Can somebody help me out? 有人可以帮我吗?
May be like this? 可能是这样吗?
var ndays = Math.floor(sec/86400);
var nhours = Math.floor((sec%86400)/3600);
var nminutes = Math.floor(((sec%86400)%3600)/60);
var nseconds = ((sec%86400)%3600)%60;
return ndays + " days " + nhours + " hours " + nminutes + " minutes " + nseconds + " seconds";
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.