Ruby to Javascript =>日期计算难题

时间:2015-01-03 16:40:54

标签: javascript ruby node.js coffeescript

我正在尝试将以下Ruby代码段重写为JavaScript,但输出并不是我所期望的。

这是原始的Ruby代码:

require 'date'
moment = DateTime.new(2014, 9, 27, 0, 0, 0, DateTime.now.offset)
intervals = [['day', 1], ['hour', 24], ['minute', 60], ['second', 60]]
elapsed = DateTime.now - moment
tense = elapsed > 0 ? 'since' : 'until'
interval = 1.0
parts = intervals.collect do |name, new_interval|
  interval /= new_interval
  number, elapsed = elapsed.abs.divmod(interval)
  "#{number.to_i} #{name}#{'s' unless number == 1}"
end
puts "#{parts.join(', ')} #{tense} that moment."

这将输出“x天,x小时,x秒以来的那一行”。

我当前的JavaScript代码是:

// Input: Nov 18 19:01:41 2014 MST
var since = function(date) {
  var elapsed, interval, intervals, parts, tense;
  date = Date.parse(date);
  intervals = [['day', 1], ['hour', 24], ['minute', 60], ['second', 60]];
  elapsed = (new Date).getTime() - date;
  tense = elapsed > 0 ? 'since' : 'until';
  interval = 1.0;
  parts = intervals.map(function(item) {
    var number;
    interval /= item[1];
    number = elapsed % interval;
    elapsed = Math.floor(elapsed / interval);
    return number + " " + item[0] + (number !== 1 ? 's' : void 0);
  });
  return console.log(parts.join(', ') + " " + tense);
};

然而,这不是预期的输出,而是给了我一些完全没有意义的东西:

0 days, 2.187114859220074e-7 hours, 0.000011154285782022377 minutes, 0.0000022063280195997896 seconds since that moment.

我尝试elapsed /= 1000,但输出仍然不是预期的输出。

1 个答案:

答案 0 :(得分:2)

请参阅下面的代码中的注释以获取修复:

var since = function(date) {
  var elapsed, interval, intervals, parts, tense;
  date = Date.parse(date);    
  intervals = [['day', 1], ['hour', 24], ['minute', 60], ['second', 60]];
  elapsed = (new Date).getTime() - date;

  // "elapsed" is in milliseconds when it was in days (Rational) in the
  // Ruby version. Fix:
  elapsed = elapsed / 1000 / 3600 / 24;

  tense = elapsed > 0 ? 'since' : 'until';
  interval = 1.0;

  parts = intervals.map(function(item) {
    var number;
    interval /= item[1];

    // You mixed "number" with "elapsed", and calculated them in
    // the wrong order. Fix:
    number  = Math.floor(elapsed / interval);
    elapsed = elapsed % interval;

    return number + " " + item[0] + (number !== 1 ? 's' : '');
  });

  return console.log(parts.join(', ') + " " + tense);
};

since('Sep 27 19:01:41 2014 MST');
97 days, 15 hours, 33 minutes, 21 seconds since