My translation from Ruby to JavaScript gives different results -


i'm writing function accepts future date , returns string in form "x weeks, y days, z hours" representing countdown date. approach is:

  1. get number of seconds separating 2 dates subtracting future date's epoch time today's epoch time.
  2. divide number of seconds 604800 (the number of seconds in week). store result weeks, , redefine seconds remainder (which ruby's divmod does).
  3. do same thing days, hours, , minutes.

first wrote in ruby, works:

def time_countdown(*date_string)   seconds = time.new(*date_string).to_i - time.now.to_i    weeks, seconds = seconds.divmod 604800   days, seconds = seconds.divmod 86400   hours, seconds = seconds.divmod 3600   minutes, seconds = seconds.divmod 60    return "#{weeks} weeks, #{days} days, #{hours} hours."    end 

i translated javascript same approach except following:

  • since javascript lacks divmod, did manually, first setting weeks/days/hours , setting seconds remainder.
  • i need use math.floor because javascript exclusively uses floats.
  • i divide epoch times 1,000 since js uses milliseconds epoch timestamps unlike ruby.
  • my js function expects receive epochtime integer since haven't learnt how pass around arbitrary-length argument lists in js.

the code is:

function timecountdown(epochtime) {     var seconds = epochtime/1000 - new date().gettime() / 1000;      var weeks = math.floor(seconds / 604800);     seconds = seconds % 604800;     var days = math.floor(seconds / 86400);     seconds = seconds % 86400;     var hours = math.floor(seconds / 3600);     seconds = seconds % 3600;      return weeks + " weeks, " + days + " days, " + hours + " hours."; } 

for date 2015,6,19, of june 1st, js gives "6 weeks, 5 days, 21 hours" , ruby gives "2 weeks, 3 days, 6 hours". can't figure out difference arises. point out mistake?

yet if feed date 2015,6,19 both functions, being june 1st write this, js tells me 6 weeks, 5 days, 21 hours , ruby tells me 2 wweeks, 3 days, 6 hours.

you haven't shown how you're doing that, guess you're doing:

timecountdown(new date(2015, 6, 19)); 

...but in javascript, month numbers start 0, not 1, june month 5, not 6:

timecountdown(new date(2015, 5, 19)); // --------------------------^ 

example:

function timecountdown(epochtime) {      var seconds = epochtime/1000 - new date().gettime() / 1000;        var weeks = math.floor(seconds / 604800);      seconds = seconds % 604800;      var days = math.floor(seconds / 86400);      seconds = seconds % 86400;      var hours = math.floor(seconds / 3600);      seconds = seconds % 3600;        return weeks + " weeks, " + days + " days, " + hours + " hours.";  }  snippet.log("july 19th: " + timecountdown(new date(2015, 6, 19)));  snippet.log("june 19th: " + timecountdown(new date(2015, 5, 19)));
<!-- script provides `snippet` object, see http://meta.stackexchange.com/a/242144/134069 -->  <script src="http://tjcrowder.github.io/simple-snippets-console/snippet.js"></script>


Comments

Popular posts from this blog

angularjs - ADAL JS Angular- WebAPI add a new role claim to the token -

php - CakePHP HttpSockets send array of paramms -

node.js - Using Node without global install -