Home > Software engineering >  how to get the dates in the last 24 hours from an array of dates in javascript
how to get the dates in the last 24 hours from an array of dates in javascript

Time:08-27

These are the dates i have in this format coming from a Mysql database, where i need to dynamically get all the fields in this array that fit in the last 24 hour window of now. the issue is that the js date comes in a different format, so i am having a hard time comparing them in a clean and efficient way.

This is the date array :

let userTransactions = [
{
  id: 1,
  created_at: "2022-08-18 12:15:12",
  updated_at: "2022-08-19 12:15:12",
},

{
  id: 2,
  created_at: "2022-08-19 10:15:12",
  updated_at: "2022-08-19 12:15:12",
},
{
  id: 3,
  created_at: "2022-08-19 16:15:12",
  updated_at: "2022-08-19 12:15:12",
},
{
  id: 4,
  created_at: "2022-08-19 05:15:12",
  updated_at: "2022-08-19 12:15:12",
},
{
  id: 5,
  created_at: "2022-08-19 11:15:12",
  updated_at: "2022-08-19 12:15:12",
},
{
  id: 6,
  created_at: "2022-08-19 08:15:12",
  updated_at: "2022-08-19 12:15:12",
},


];

this is the function :

const filterTo24Hours = (userTransactions) => {
let dayTransactions = userTransactions.filter((item) => {
  let date = new Date();

  // this if statement is written to show what i want to have achieved, this is not working 
  if (date >= item.created_at && date - 1 <= item.created_at) {
    //Do something here
    console.log(item.created_at);
  }
});
return dayTransactions;
 }

CodePudding user response:

Without heavy additional libraries like moment.js you can transform your dates to milliseconds to compare them. I changed your first item of array with created_at within 24 hours so it returns something.

let userTransactions = [
  {
    id: 1,
    created_at: "2022-08-26 12:15:12",
    updated_at: "2022-08-26 12:15:12",
  },

  {
    id: 2,
    created_at: "2022-08-19 10:15:12",
    updated_at: "2022-08-19 12:15:12",
  },
  {
    id: 3,
    created_at: "2022-08-19 16:15:12",
    updated_at: "2022-08-19 12:15:12",
  },
  {
    id: 4,
    created_at: "2022-08-19 05:15:12",
    updated_at: "2022-08-19 12:15:12",
  },
  {
    id: 5,
    created_at: "2022-08-19 11:15:12",
    updated_at: "2022-08-19 12:15:12",
  },
  {
    id: 6,
    created_at: "2022-08-19 08:15:12",
    updated_at: "2022-08-19 12:15:12",
  },
];

const filterTo24Hours = (userTransactions) => {
  let date = (new Date()).getTime() - 24 * 60 * 60 * 1000;
  let dayTransactions = userTransactions.filter((item) => (new Date(item.created_at)).getTime() >= date);
  return dayTransactions;
}

console.log(filterTo24Hours(userTransactions));

CodePudding user response:

const filterTo24Hours = (userTransactions) => {
    let dayTransactions = userTransactions.filter((item) => {
        // reduce 1 day from current time
        let date = Date.now() - 86400000;
        // this if statement is written to show what i want to have achieved, this is not working
        // created date converted to UTC time by default
        if (date < new Date(item.created_at).getTime()) {
            //Do something here
            //console.log(item.created_at);
            return item
        }
    });
    return dayTransactions;
}
  • Related