MomentJs减去实际时间

时间:2016-07-01 11:44:57

标签: javascript momentjs

我需要从时间变量中减去实际时间(格式为hh:mm:ss)并以秒为单位获得结果,例如:

var time1 = "13:40:00";
var time1 = moment(time1, "HH:mm:ss");
var timeNow = ?
var time2 = time1 - timeNow  // expressed in s

我怎样才能做到这一点?

3 个答案:

答案 0 :(得分:0)

我不确定我是否完全了解您的需求,但尝试一下

var u, d, l, r;
u = d = l = r = false;

function handleKeyPressed(e) {
  switch (String.fromCharCode(e.which)) {
    case "w":
      u = true;
      break;
    case "a":
      l = true;
      break;
    case "s":
      d = true;
      break;
    case "d":
      r = true;
      break;
  }
}

function handleKeyReleased(e) {
  switch (String.fromCharCode(e.which)) {
    case "w":
      u = false;
      break;
    case "a":
      l = false;
      break;
    case "s":
      d = false;
      break;
    case "d":
      r = false;
      break;
  }
}

function moveModel(_u, _d, _l, _r, obj) {
  if (_u && !detectaColisao(obj.position.x, obj.position.y + SCALE)) {
    obj.position.y += SCALE;
  }
  if (_d && !detectaColisao(obj.position.x, obj.position.y + SCALE)) {
    obj.position.y -= SCALE;
  }
  if (_l && !detectaColisao(obj.position.x, obj.position.y + SCALE)) {
    obj.position.x -= SCALE;
  }
  if (_r && !detectaColisao(obj.position.x, obj.position.y + SCALE)) {
    obj.position.x += SCALE;
  }
}

function anima() {
  var PacMan = scene.getObjectByName('PacMan');
  moveModel(u, d, l, r, PacMan);
  var delta = clock.getDelta();
  orbitCamera.update(delta);
  requestAnimationFrame(anima);
  rendere();
}

答案 1 :(得分:0)

您可以使用时刻diff来获取两个时刻对象之间的差异。在你的情况下:



var time1 = '13:40:00';
var diff = moment().diff(moment(time1, 'HH:mm:ss'), 'seconds');
console.log(diff);

<script src="//cdnjs.cloudflare.com/ajax/libs/moment.js/2.13.0/moment.min.js"></script>
&#13;
&#13;
&#13;

答案 2 :(得分:0)

试试这个:

var time1 = moment.duration("24:40:00", "HH:mm:ss").asSeconds();
var timeNow =moment.duration(moment().format('HH:mm:ss'),"HH:mm:ss").asSeconds();
var log = time1-timeNow;