使用力矩获取日期和当前日期之间的时差

时间:2019-01-15 17:19:01

标签: javascript datetime react-native momentjs

嗨,我正在尝试获取两个日期之间的时差。我编写了一个函数来执行此操作,但是当我将传入的日期从共享的首选项转换为时刻日期时,它会给我一个数字。

在我的本机项目中使用它

我尝试使用手动输入日期作为字符串进行同样的操作,并且效果很好。

我在做什么错了?

//此功能无法正常工作(我希望该功能返回时差)

export const getTimeDiffWithCurrentTime = (lastLoggedInDateTime) => {
  var moment = require('moment');
//***PROBLEM IS HERE IN THE BELLOW LINE WHERE THE OUTPUT IS 1547571895000***
  var momentLastLoggedInDateTime = moment(lastLoggedInDateTime, "DD-MM-YYYY HH:mm:ss");
  var currentDateTime = moment().format("DD-MM-YYYY HH:mm:ss");
  consoleLog(' lastLoggedInDateTime - ' + lastLoggedInDateTime + 'momentLastLoggedInDateTime - ' + momentLastLoggedInDateTime
    + ' currentDateTime - ' + currentDateTime);
  var timeDifference = currentDateTime.diff(momentLastLoggedInDateTime);
  consoleLog("Time difference - " + timeDifference);
  return timeDifference;
}

输出: lastLoggedInDateTime - 15-01-2019 17:04:55momentLastLoggedInDateTime - 1547571895000 currentDateTime - 15-01-2019 17:04:57

error -TypeError: currentDateTime.diff is not a function

有效的测试功能

export const getTimeDiff = () => {
  var moment = require('moment');
  var now  = moment("04-09-2013 15:00:00", "DD-MM-YYYY HH:mm:ss");
  var then = moment("04-09-2013 14:59:40", "DD-MM-YYYY HH:mm:ss");
  var timeDifference = now.diff(then);
  consoleLog("Time difference - " + timeDifference);
}

输出: Time difference - 20000

我想通过传递日期并获得差值来使函数getTimeDiffWithCurrentTime正常工作。

非常感谢您的帮助

R

更新

使其正常工作。没什么要注意的。

有人建议只使用var momentLastLoggedInDateTime = moment(lastLoggedInDateTime);

但这给了我类似于

的警告

Deprecation warning: value provided is not in a recognized ISO format. moment construction falls back to js Date(), which is not reliable across all browsers and versions. Non ISO date formats are discouraged and will be removed in an upcoming major release. Please refer to http://momentjs.com/guides/#/warnings/js-date/ for more info. Arguments: [0] _isAMomentObject: true, _isUTC: true, _useUTC: true, _l: undefined, _i: 2016-9-26 19:30, _f: undefined, _strict: undefined, _locale: [object Object]

所以我不得不使用var momentLastLoggedInDateTime = moment(lastLoggedInDateTime, "DD-MM-YYYY HH:mm:ss");

这给了我1547571895000的价值

我只需要对函数'getTimeDiffWithCurrentTime'进行更改,就是将var currentDateTime = moment().format("DD-MM-YYYY HH:mm:ss");更改为var currentDateTime = moment();

所以工作功能是

export const getTimeDiffWithCurrentTime = (lastLoggedInDateTime) => {
  var moment = require('moment');
  var momentLastLoggedInDateTime = moment(lastLoggedInDateTime, "DD-MM-YYYY HH:mm:ss");
  var currentDateTime = moment(); //This was the change required
  consoleLog(' lastLoggedInDateTime - ' + lastLoggedInDateTime + 'momentLastLoggedInDateTime - ' + momentLastLoggedInDateTime
    + ' currentDateTime - ' + currentDateTime);
  var timeDifference = currentDateTime.diff(momentLastLoggedInDateTime);
  consoleLog("Time difference - " + timeDifference);
  return timeDifference;
}

输出

lastLoggedInDateTime - 15-01-2019 18:12:26momentLastLoggedInDateTime - 1547575946000 currentDateTime - 1547575952574 Time difference - 6574

谢谢

2 个答案:

答案 0 :(得分:1)

根据documentation of moment,基本上没有参数,diff函数返回milliseconds中的数字,您可以使用yearsmonths,{如果要使用其他时间单位,请使用{1}},weeksdayshoursminutes

考虑:

  

默认情况下,moment#diff会将结果截断为零个小数位,并返回一个整数。如果需要浮点数,则将true作为第三个参数传递。

检查一段显示其他类型单位的代码。

seconds
const getTimeDiff = (differenceIn = 'milliseconds', floating= false) => {
  var now = moment("04-09-2013 15:00:00", "DD-MM-YYYY HH:mm:ss");
  var then = moment("04-09-2013 14:59:40", "DD-MM-YYYY HH:mm:ss");
  //this return the difference between now and then in milliseconds as default
  var timeDifference = now.diff(then, differenceIn, floating);
  console.log("Time difference - " + timeDifference + ' ' + differenceIn);
}

getTimeDiff();
getTimeDiff('seconds');
getTimeDiff('seconds', true);
getTimeDiff('minutes');
getTimeDiff('minutes', true);
getTimeDiff('hours');
getTimeDiff('hours', true);
getTimeDiff('days');
getTimeDiff('days', true);
getTimeDiff('weeks');
getTimeDiff('weeks', true);
getTimeDiff('months');
getTimeDiff('months', true);
getTimeDiff('years');
getTimeDiff('years', true);

在代码上,检查您是否尝试执行<script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.23.0/moment.js"></script>中的diff。 基本上,您在应用String之前正在做format

diff
const getTimeDiffWithCurrentTime = (lastLoggedInDateTime) => {
  //***PROBLEM IS HERE IN THE BELLOW LINE WHERE THE OUTPUT IS 1547571895000***
  var momentLastLoggedInDateTime = moment(lastLoggedInDateTime, "DD-MM-YYYY HH:mm:ss");
  //var currentDateTime = moment().format("DD-MM-YYYY HH:mm:ss"); IF YOU DO THIS, you get a STRING
  var currentDateTime = moment()
  console.log(' lastLoggedInDateTime - ' + lastLoggedInDateTime + 'momentLastLoggedInDateTime - ' + momentLastLoggedInDateTime +
    ' currentDateTime - ' + currentDateTime);
  var timeDifference = currentDateTime.diff(momentLastLoggedInDateTime);
  console.log("Time difference - " + timeDifference);
  return timeDifference;
}

const newDate = new Date(new Date() - 10000); //minus 10 seconds


getTimeDiffWithCurrentTime(newDate);

答案 1 :(得分:0)

var now  = moment(new Date());
var then = moment("04-09-2013 14:59:40", "DD-MM-YYYY HH:mm:ss");
var timeDifferenceInSeconds = now.diff(then, 'seconds');
var timeDifferenceInHours = now.diff(then, 'hours');
var timeDifferenceInDay = now.diff(then, 'days');
console.log(timeDifferenceInHours, timeDifferenceInSeconds, timeDifferenceInDay);
<script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.23.0/moment.min.js"></script>