根据天数查找周期。

时间:2017-08-10 07:08:10

标签: javascript math

我有hours的动态数字,我希望他们能在几周内完成 例如,假设我得到196小时的结果

我已将此转换为以下日期:

var daysX = hours / 24;

现在,如果daysx等于或小于7,则应将周值分配为1,如果daysx大于7但小于14,则周应为2,因此上。

hours将是完全动态的,所以我无法应用if else&相比。

有没有办法根据小时数动态应用周数?

任何想法都将不胜感激!提前谢谢..

4 个答案:

答案 0 :(得分:2)

您可以将小时数除以周长,然后使用Math.ceil进行四舍五入。



function getWeeks(hours) {
    return Math.ceil(hours / 24 / 7);
}

console.log(getWeeks(196)); // two weeks
console.log(getWeeks(169)); // two weeks
console.log(getWeeks(168)); // one week




答案 1 :(得分:2)

let hours = 200;
let result = Math.ceil(hours/(24*7))
console.log(result);

这应该适合你

答案 2 :(得分:0)

您可以将 //Donwload images from server $app->GET('/downloadMedia/{filename}', function ($request, $response, $args) { $file = 'upload/images/'.$args['filename']; if(file_exists($file)) { $response ->withHeader('Content-Description', 'File Transfer') ->withHeader('Content-Type','application/octet-stream') ->withHeader('Content-Disposition', 'attachment;filename="'.basename($file).'"') ->withHeader('Expires', '0') ->withHeader('Content-Transfer-Encoding', 'binary') ->withHeader('Cache-Control', 'must-revalidate') ->withHeader('Pragma', 'public') ->withHeader('Content-Length', filesize($file)); readfile($file); exit; } }); 乘以传递给24的{​​{1}}除以24

的小时数除以
Math.round()

答案 3 :(得分:-1)

您可以执行以下操作:

var hours = 196
var days = hours / 24;
var week = Math.floor(days /7);
days = days % 7;
相关问题