javascript - 使用 moment 获取日期和当前日期之间的时间差

标签 javascript datetime react-native momentjs

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

在我的 React Native 项目中使用它

我尝试了同样的方法,手动输入日期作为字符串,效果很好。

我在这里做错了什么?

//这个函数没有按预期工作(我希望这个函数返回时间差)

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

错误-TypeError:currentDateTime.diff不是函数

测试功能是否有效

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);
}

输出: 时差 - 20000

我想让函数 getTimeDiffWithCurrentTime 通过传递日期并获取差异来工作。

非常感谢您的帮助

R

更新

成功了。有几点需要注意。

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

但这给了我类似的警告

弃用警告:提供的值不是可识别的 ISO 格式。 moment 构造回落到 js Date(),这在所有浏览器和版本中并不可靠。不鼓励采用非 ISO 日期格式,并将在即将发布的主要版本中将其删除。请参阅 http://momentjs.com/guides/#/warnings/js-date/了解更多信息。参数:[0] _isAMomentObject:true,_isUTC:true,_useUTC:true,_l:未定义,_i:2016-9-26 19:30,_f:未定义,_strict:未定义,_locale:[对象对象]

所以我必须使用 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 时差 - 6574

谢谢

最佳答案

根据documentation of moment ,基本上没有参数,diff函数返回一个以毫秒为单位的数字,可以使用小时分钟(如果您想使用)不同的时间单位。

考虑:

By default, moment#diff will truncate the result to zero decimal places, returning an integer. If you want a floating point number, pass true as the third argument.

检查显示其他类型单位的工作代码。

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>

在您的代码中,检查您是否正在尝试对 String 进行diff。 基本上,您是在应用 diff 之前执行format

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);
<script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.23.0/moment.js"></script>

关于javascript - 使用 moment 获取日期和当前日期之间的时间差,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54203859/

相关文章:

javascript - 在 javascript 中不使对象保持事件状态的引用列表

javascript - AngularJS $watch 函数重复调用 - 如何防止它

mysql - 在 Java ee 应用程序中使用日期时间

react-native - "Unable to resolve module"在最近的 react-native 版本中出现 debuggerWorker 错误

android - 在 ReactNative 项目中!为什么我的 ANDROID Gradle 版本不同

javascript - AngularJS 使用 ng-resource 和多种服务器轮询方法

javascript - "Semi-controlled" react 中的组件

python - 保持奇怪的时间格式,并在 python 中向其添加值

python - 解码为字符串

react-native - React Native TypeError : undefined is not an object (evaluating 'props.getItem' )