开发者

Whats the mathematical formula for converting days into weeks

I don't want decimal points like 65 days = 6.2weeks. I want开发者_JAVA百科 65 days = 6 weeks 1 day

I can't use any libraries (not homework)


Assuming Java (I don't know Javascript):

weeks    = days / 7;
days_out = days % 7;


Simply use a modulus to get the amounts of days remaining then divide the rest by 7.

var daysLeft = days % 7;
var weeks = Math.floor(days / 7);

The code above works in both Java and JavaScript (remember, other than having "Java" in the name, Java and JavaScript are two different, unrelated languages). It might be more appropriate to declare the variables as an int in Java however.


In JavaScript:

function daysToWeeks(days)
{
    return {
        weeks: Math.floor(days/7),
        days: days % 7
    };
}


What language?

If javascript you could do it like:

Math.floor(65 / 7)     -> 9 weeks

And to get how many days into the week:

65 % 7                 -> 2 days
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜