javascript - JS weeks, hours and days to seconds.. and back again -
i trying convert number of weeks, days , hours seconds , convert them again.
when convert them back, days , hours not correct:
var weeks = 3, days = 5, hours = 1; //convert seconds sec_in_w = weeks * 604800, sec_in_d = days * 86400, secs_in_h = hours * 3600, secs = sec_in_w + sec_in_d + secs_in_h; //convert weeks, days, , hours new_w = math.floor(secs / 604800); secs -= new_w; new_d = math.floor(secs / 86400); secs -= new_d; new_h = math.floor(secs / 3600); console.log('weeks: ' + new_w); console.log('days: ' + new_d); console.log('hour: ' + new_h);
demo:
//convert weeks, days, , hours new_w = math.floor(secs / 604800); secs = secs % 604800; new_d = math.floor(secs / 86400); secs = secs % 86400; new_h = math.floor(secs / 3600);
using modulus gives remainder.
Comments
Post a Comment