1

I have an array of objects. Each object has a date property. I am trying to create a function where I append every item to a new array if the item's date is equal to today's date.

Also, I am not sure if the for loop is the most efficient approach for this, this list will never be more than a few hundred items though.

My function:

todayListItems() {
    const todayItems = [];
    const todayDate = moment(new Date()).format('dd-mm-YYYY');
    for (let i = 0; i < myArray.length; i++) {
      const itemDate = moment(myArray[i].date).format('dd-mm-YYYY');
      if (itemDate === todayDate) {
        todayItems.push(myArray[i]);
      }
    }
    console.log(todayItems);
    return todayItems;
  }

This function runs but even if there is an item with today's date nothing will be pushed to the array.

Dale K
  • 25,246
  • 15
  • 42
  • 71
goolius_boozler
  • 233
  • 5
  • 21
  • Why not just user `Array.prototype.filter` cause that seems to be exactly what you are doing? As for it not working, any chance you could give us the inputs? – Icepickle Oct 22 '19 at 09:48
  • *not sure if the for loop is the most efficient approach for this* for performance, `for` is the best. For readability, `filter` is more preferred. – Rajesh Oct 22 '19 at 09:50
  • Recommendation with your code. Since you are using javascript you can use this for shorter code. myArray.forEach(data => { }) use forEach instead of for loop – Renato Manalili Oct 22 '19 at 09:51
  • 2
    @RenatoManalili forEach when you want to filter an array is just pure madness ;) – Icepickle Oct 22 '19 at 09:52
  • @Rajesh `filter` is just a some syntactical sugar for a `for` loop. It's no more efficient. `filter`, `forEach` and `for` all do the same thing in slightly different ways, there is no "better" – Liam Oct 22 '19 at 09:53
  • Possible duplicate of [Moment JS - check if a date is today or in the future](https://stackoverflow.com/questions/21284312/moment-js-check-if-a-date-is-today-or-in-the-future) – Liam Oct 22 '19 at 09:54
  • Please read again. Performance -> `for`. Readability -> `filter` etc. Any functional approach will add some extra processing – Rajesh Oct 22 '19 at 09:54
  • @Icepickle I know that there is a slight difference between using our recommendations. The important thing is we helped him solve his problem. – Renato Manalili Oct 22 '19 at 09:56

2 Answers2

1

You're formatting wrongly. mm is minutes. The formatting should be DD-MM-YYYY, see https://momentjs.com/docs/#/displaying/format/

Jasmonate
  • 752
  • 1
  • 8
  • 18
  • This should be a comment as well, though correctly pointed out – Rajesh Oct 22 '19 at 09:56
  • Why the downvote? It's a valid answer that explains why OP's code doesn't work as expected. There are better ways to compare dates; this might be bad practice but it isn't invalid. – Jasmonate Oct 22 '19 at 10:01
  • This fixed it. I'm sure there's a much cleaner way for me to do it but this made it work. – goolius_boozler Oct 22 '19 at 10:01
  • Note: @Fraction's answer is better. Keeping this up to show how the original code is wrong but please use their solution. – Jasmonate Oct 22 '19 at 10:09
1

You can use Array.filter() and moment#isSame with the second parameter to limit the granularity to a unit other than milliseconds:

function todayListItems(arr) {
  return arr.filter(obj => moment().isSame(obj.date, 'day'));
}

todayListItems(myArray);

Demo:

function todayListItems(arr) {
  return arr.filter(obj => moment().isSame(obj.date, 'day'));
}

const myArray = [
  { date: '2019-10-14T12:10:00Z'},
  { date: new Date() },
  { date: moment().minutes(120).format('YYYY-MM-DDTHH:mm:ss') },
  { date: new Date('2019-10-23T01:00:00') },
];

console.log(todayListItems(myArray));
<script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.24.0/moment.min.js"></script>
Fraction
  • 11,668
  • 5
  • 28
  • 48
  • Is there a way to do this as well for items that are tomorrow? Like could you take the 'day' argument and add one day to it to get a list of items that are tomorrow? – goolius_boozler Oct 22 '19 at 10:13
  • 1
    You can do it like this : `arr.filter(obj => moment().add(1, 'day').isSame(obj.date, 'day')` – Fraction Oct 22 '19 at 10:29