Home > Net >  Getting An Array of Items within the last 24 hours from a given data using JavaScript
Getting An Array of Items within the last 24 hours from a given data using JavaScript

Time:09-24

I have some given data where I'm to return some set of arrays with just 24 hours using the time stamp in the given array. I tried the code below but it was returning the same data to me again. I hope I'm not doing

const Array_1 = [
  { class_1: 4, HeadTeacher: "Mrs Joe", time_stamp: "2021-09-23 11:31:41" },
];
const Array_2 = [
  { class_1: 10, HeadTeacher: "Loveth", time_stamp: "2021-09-16 15:16:40" },
];
const Array_3 = [
  { class_1: 1, HeadTeacher: "Itunu", time_stamp: "2021-09-22 14:52:32" },
];
const Array_4 = [
  { class_1: 1, HeadTeacher: "John", time_stamp: "2021-09-21 07:07:30" },
];

const result = [...Array_1, ...Array_2, ...Array_3, ...Array_4];

const Final = result.filter((item, index) => {
  return new Date(item.time_stamp - 86400 * 1000);
});

console.log(Final);

it right.

CodePudding user response:

Please use this code. I get the result that the date is greater than (now time - 1 day).

const Array_1 = [
  { class_1: 4, HeadTeacher: "Mrs Joe", time_stamp: "2021-09-23 11:31:41" },
];
const Array_2 = [
  { class_1: 10, HeadTeacher: "Loveth", time_stamp: "2021-09-16 15:16:40" },
];
const Array_3 = [
  { class_1: 1, HeadTeacher: "Itunu", time_stamp: "2021-09-22 14:52:32" },
];
const Array_4 = [
  { class_1: 1, HeadTeacher: "John", time_stamp: "2021-09-21 07:07:30" },
];

const result = [...Array_1, ...Array_2, ...Array_3, ...Array_4];

const Final = result.filter((item) => (new Date(item.time_stamp).getTime() > Date.now() - 24 * 60 * 60 * 1000));

console.log(Final);

CodePudding user response:

I would first calculate the the timestamp for a day ago and then compare the same with the timestamp in the object

const Array_1 = [
  { class_1: 4, HeadTeacher: "Mrs Joe", time_stamp: "2021-09-23 11:31:41" },
];
const Array_2 = [
  { class_1: 10, HeadTeacher: "Loveth", time_stamp: "2021-09-16 15:16:40" },
];
const Array_3 = [
  { class_1: 1, HeadTeacher: "Itunu", time_stamp: "2021-09-22 14:52:32" },
];
const Array_4 = [
  { class_1: 1, HeadTeacher: "John", time_stamp: "2021-09-21 07:07:30" },
];

const result = [...Array_1, ...Array_2, ...Array_3, ...Array_4];

const DAY = 24 * 60 * 60 * 1000;
const aDayAgo = new Date(Date.now() - DAY);

const final = result.filter( ({time_stamp}) => new Date(time_stamp) > aDayAgo );
console.log(final)

CodePudding user response:

const Array_1 = [
  { class_1: 4, HeadTeacher: "Mrs Joe", time_stamp: "2021-09-23 11:31:41" },
];
const Array_2 = [
  { class_1: 10, HeadTeacher: "Loveth", time_stamp: "2021-09-16 15:16:40" },
];
const Array_3 = [
  { class_1: 1, HeadTeacher: "Itunu", time_stamp: "2021-09-22 14:52:32" },
];
const Array_4 = [
  { class_1: 1, HeadTeacher: "John", time_stamp: "2021-09-21 07:07:30" },
];

const result = [...Array_1, ...Array_2, ...Array_3, ...Array_4];

const Final = result.filter((item, index) => {
    let d = new Date(item.time_stamp).getTime(),
        passedTimeInHours = (((Date.now() - d) / 1000) / 60) / 60
    return passedTimeInHours < 24
});
  • Related