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:

http://codepen.io/anon/pen/avzwbp

//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

Popular posts from this blog

java - Date formats difference between yyyy-MM-dd'T'HH:mm:ss and yyyy-MM-dd'T'HH:mm:ssXXX -

c# - Get rid of xmlns attribute when adding node to existing xml -