【问题标题】:getting time difference between a date and current date using moment使用时刻获取日期和当前日期之间的时间差
【发布时间】:2019-01-15 17:19:01
【问题描述】:

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

在我的 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

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

谢谢

【问题讨论】:

  • 你的问题是什么?
  • @JeffUK 我想让第一个功能正常工作抱歉不清楚我会更新它

标签: javascript datetime react-native momentjs


【解决方案1】:

根据documentation of moment,基本不带参数,diff函数在milliseconds中返回一个数字,可以使用yearsmonthsweeksdayshoursminutesseconds,如果您想使用不同的时间单位。

考虑:

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

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

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>

在您的代码中,检查您是否正在尝试执行 diffString。 基本上,在应用 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>

【讨论】:

  • 这就是我之前尝试过的,然后根据文档我从var momentLastLoggedInDateTime = moment(lastLoggedInDateTime); 转换为var momentLastLoggedInDateTime = moment(lastLoggedInDateTime, "DD-MM-YYYY HH:mm:ss");
  • @BRDroid 在构造函数上使用该格式是有效的,因为moment(date, format) 返回一个Moment Date,但如果你采用Moment Date 并应用format,你将得到一个string。检查答案,我将其编辑为具有constructor 上的格式,但您仍然需要删除格式。仅当您想以正确的方式显示//保存//发送日期时才使用format
  • @BRDroid 你检查我的回答和评论了吗?
【解决方案2】:

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>

【讨论】:

  • 你好 @Avanthika 我希望函数 getTimeDiffWithCurrentTime 工作
猜你喜欢
  • 2011-12-10
  • 1970-01-01
  • 2023-03-26
  • 1970-01-01
  • 1970-01-01
  • 2019-04-24
  • 2012-12-04
  • 1970-01-01
相关资源
最近更新 更多