Javascript:检查时间戳记是否早于24小时

时间:2020-08-25 10:24:45

标签: javascript node.js datetime mongoose

我需要获取bitbucket存储库并根据 updated_on 时间戳对其进行过滤。

目标:用于过滤最近24小时内更新的内容。

这是我从API获得的信息: updated_on:2018-01-30T11:45:32.902996 + 00:00

我的代码逻辑:

// repo's updated_on timestamp (example) 
const time = '2018-01-30T11:45:32.902996+00:00'
let currentTime = Date.now()
let updatedOn = new Date(time).getTime()

const filter = () => {
  // boolean
  return (currentTime - updatedOn) > 86400000)
}

if (filter()) {
  // NOT FILTERING // giving out last month's data as well
  console.log(updatedOn)
} 

1 个答案:

答案 0 :(得分:0)

Ciao,通过moment库,您可以通过以下方式使用isBefore函数:

let date = "2018-01-30T11:45:32.902996+00:00";
let anotherDate = moment(); // today
console.log(moment().subtract(24, 'hours').isBefore(moment(new Date(date))));
console.log(moment().subtract(24, 'hours').isBefore(moment(new Date(anotherDate))));
<script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.17.1/moment.min.js"></script>