0

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;
 }
3
  • please paste the date array Commented Aug 26, 2022 at 14:45
  • @brk my bad i just updated Commented Aug 26, 2022 at 14:49
  • What did you try to parse the string into a Date object, and what did you try to compare those two date objects? I think you'll find plenty of answers here in SO, but if you're stuck somewhere, please explain where exactly Commented Aug 26, 2022 at 14:53

2 Answers 2

1

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));

Sign up to request clarification or add additional context in comments.

Comments

0
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;
}

1 Comment

"2022-08-18 12:15:12" is not a format supported by ECMA-262 so parsing is implementation dependent. It produces an invalid date in Safari at least.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.