I ma struggling with this mathematical-based query.
I have the difference in seconds between two dates, and would like to convert the number of seconds to a fraction. I have the decimal working well, but am struggling to figure out how I can convert this value to a fraction.
So, for example, the number of weeks difference between the two timestamps is 4.285714285714286. I would like to represent this as 4 2/7
.
Can anyone suggest the most efficient way to achieve this in JavaScript?
Maybe I didn't understand your question, so this could be a very naive answer. If yes, just says so.
var
a = new Date(2011, 12, 31),
b = new Date(2011, 12, 1),
weekMilliSeconds = 7 * 24 * 60 * 60 * 1000,
fraction = (a - b) / weekMilliSeconds,
weeks = Math.floor(fraction),
days = Math.round((fraction - weeks) * 7);
console.log(
weeks + ' ' + days + '/7' // => 4 2/7
);