【问题标题】:Check if the difference in time exceeds 30 min using moment JS/angularjs使用 moment JS/angularjs 检查时间差是否超过 30 分钟
【发布时间】:2017-01-24 03:19:00
【问题描述】:

我正在使用 ag-grid,我们正在使用 moment js 进行时间操作。

这里是场景:我在 unix 时间从后端 (JSON) 获取最后更新的时间戳(如 1485120726)。我的逻辑是检查最后更新的时间戳是否少于 30 分钟。如果是这样,单元格应该是红色的。除非有新的修订,否则此突出显示将在 30 分钟后到期(返回其原始色块)。

我现在被困在如何检查当前时间戳和上次更新时间戳之间的差异。

这是我的代码:

  highlightFAA =(params):any=>{
  var CurrentDate = moment().unix();  //val 1485120726-currUnixtime
  var lastRevised= (params.node.data.lastRevised/1000); //Val 1484859960--backend is giving value in millisec so converting to sec

  console.log("currdate", CurrentDate);
  console.log("lastRevised", lastRevised);
  console.log("param node", params.node.data.lastRevised);

  //30 min is 1800 sec
  if(Math.abs(lastRevised- CurrentDate)>1800){
    return  {'background-color': 'black', 'text-align': 'center', 'font-weight': 'bold'};
  } else {
    return {'background-color': '#FFEB3B', 'text-align': 'center', 'font-weight': 'bold'};
  }

}

我不确定这个逻辑是否足够

我尝试了另一种使用diff的方法

highlightFAA = (params):any => {

  var CurrentDate = moment().format('YYYY-MM-DD HH:mm');
  var lastRevised = moment.unix(params.data.lastRevised/1000).format('YYYY-MM-DD HH:mm');

  console.log("currdate", CurrentDate);
  console.log("lastRevised", lastRevised);
  console.log("param node", params.node.data.lastRevised);

  var ms = moment(lastRevised).diff(moment(currdate));
}

但我收到错误消息,提示 diff 不能用于字符串或数字我不知道如何继续,我需要一些建议。

**这里是我处理的更新代码,以分钟为单位。

 highlightFAA = (params):any=> {

     var CurrentDate = moment().tz("UTC").format('YYYY-MM-DD HH:mm');
     var lastRevised = moment.unix(params.data.lastRevised/1000).tz("UTC").format('YYYY-MM-DD HH:mm');
     var ms = moment(CurrentDate).diff(moment(lastRevised), 'minutes');

     if(ms>30) {
         return {'background-color': '#FFEB3B', 'text-align': 'center', 'font-weight': 'bold'};
     } else {
       return {'color': 'black', 'text-align': 'center', 'font-weight': 'bold'};
     }
 }

但是这里每次我重新加载页面时都会检查时间差,但我需要一种方法来动态重新检查当前时间和上次修改时间之间的时间差,并在超过 30 分钟时间限制时立即更改颜色.

【问题讨论】:

  • but what I need is to a way to recheck the time difference between currtime and last revised time dynamically and change color as soon as it exceeds 30 min time limit 你可以使用$interval
  • 当我使用 $timeout 或间隔时,我收到错误找不到名称超时。即使我正在注入服务 var __inject = [ '$rootScope', '$compile', '$http', '$uibModal', '$timeout' ];构造函数(私有 $rootScope,私有 $compile,私有 $http,私有 $uibModal,私有超时;)
  • 您超出了此问题的范围。通常你应该做的是创建一个新问题。但是我很容易看到你的问题,把private timeout改成private $timeout

标签: javascript momentjs ag-grid


【解决方案1】:

使用.subtract 获取 30 分钟前的时间戳

let halfAnHourAgo = moment().subtract(30, 'minutes').toDate().getTime();

.toDate 会将moment 转换为纯js Date 对象。

.getTime 将返回日期的时间戳。

然后检查您的 unix 时间戳是大于还是小于 moment 给您的时间

if (yourServerDate < halfAnHourAgo) {
    // color cell red
}

【讨论】:

  • highlightFAA =(params):any=>{ var CurrentDate = moment().tz("UTC").format('YYYY-MM-DD HH:mm'); var faaLastRevisedDate = moment.unix(params.data.faaLastRevised/1000).format('YYYY-MM-DD HH:mm'); var ms = moment(CurrentDate).diff(moment(faaLastRevisedDate), 'minutes');控制台.log(“毫秒”,毫秒); if(ms>30) { return {'background-color': '#FFEB3B', 'text-align': 'center', 'font-weight': 'bold'}; } else { return {'color': 'black', 'text-align': 'center', 'font-weight': 'bold'}; } }
  • @svarog- 我使用上面的代码来检查差异。但它没有提供动态检查的方法。
猜你喜欢
  • 2017-08-27
  • 1970-01-01
  • 2014-05-11
  • 2016-02-22
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-03-09
相关资源
最近更新 更多