Convert days to weeks and days

This one is really easy. Say you want to convert a number of days to the number of weeks and remaining days.

Here’s how it works.

let num = 50

let weeks = Math.floor(num / 7)   // =7
let days = (num % 7)              // =1

So we take 50, divide it by 7 and round down to the nearest whole number. That’s 7.

Now we say 7x7=49 and 50-49 equals 1.

Result: 50 days is equal to 7 weeks and 1 day.

Can’t see it? Here’s a couple of examples.

let num = 63

let weeks = Math.floor(num / 7)   // =9
let days = (num % 7)              // =0
let num = 34

let weeks = Math.floor(num / 7)   // =4
let days = (num % 7)              // =6

I should have really explained the operators:

  • / – divide
  • % – get the remainder of dividing the two numbers

Share