0

I need to fetch bitbucket repositories and filter them according to the updated_on timestamp.

GOAL: Is to filter the ones which are updated in last 24 hours.

This is what I get from API : updated_on: 2018-01-30T11:45:32.902996+00:00

My code logic:

// 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)
} 
Karan Kumar
  • 2,678
  • 5
  • 29
  • 65
  • Given that the timestamp is probably the only format that is both supported by ECMA-262 and reliably parsed by current implementations, plus you want exactly 24 hours then `(new Date() - Date.parse(time)) < 8.64e7` will do. There are [many similar questions](https://stackoverflow.com/search?q=%5Bjavascript%5D+difference+between+two+dates). – RobG Aug 26 '20 at 04:04

1 Answers1

0

Ciao, with moment library you could use isBefore function in this way:

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>
Giovanni Esposito
  • 10,696
  • 1
  • 14
  • 30