2

Compare dates from an array object with the current date. the current date is taken from MongoDB.

{"specialData": [
        {
            "content": {
                "dateAdded": "2017-12-26T22:21:37+00:00"
            }
        },
        {
            "content": {
                "dateAdded": "2018-01-12T22:21:37+00:00"
            }
        }
]
}

compare the content.dateAdded from the array object with the current date coming from MongoDB. current date coming from catalogue.metaData.lastUpdatedDate

let result = _.find(rokuFeeds.tvSpecials, function (n) {
      if (new Date(n.content.dateAdded) < new Date(catalogue.metaData.lastUpdatedDate)) {
        return true;
      }
    });

I'm trying like this

sarangi
  • 23
  • 3
  • 1
    Does this answer your question? [Compare two dates with JavaScript](https://stackoverflow.com/questions/492994/compare-two-dates-with-javascript) – Cuong Vu Apr 09 '22 at 08:12
  • it's not @CuongVu – sarangi Apr 09 '22 at 08:14
  • Does this answer your question? [Compare Dates In Mongoose Doc and Express Current Date](https://stackoverflow.com/questions/55755379/compare-dates-in-mongoose-doc-and-express-current-date) – user2226755 Apr 09 '22 at 08:19

2 Answers2

1

You can compare dates by using getTime() method.

const obj = {
  "specialData": [{
    "content": {
      "dateAdded": "2017-12-26T22:21:37+00:00"
    }
  },
  {
    "content": {
      "dateAdded": "2018-01-12T22:21:37+00:00"
    }
  }]
}

const d1 = new Date(obj.specialData[0].content.dateAdded).getTime();
const d2 = new Date(obj.specialData[1].content.dateAdded).getTime();

Now simply compare them, using comparison operators > < >= <=

d1 > d2;  //false
d1 < d2;  //true
d1 >= d2; //false
d1 <= d2; //true
nurmdrafi
  • 419
  • 4
  • 11
0

You can use new Date(obj.specialData[0].content.dateAdded); to convert string to date object.

const obj = {
  "specialData": [{
    "content": {
      "dateAdded": "2017-12-26T22:21:37+00:00"
    }
  },
  {
    "content": {
      "dateAdded": "2018-01-12T22:21:37+00:00"
    }
  }]
}
const d1 = new Date(obj.specialData[0].content.dateAdded);
const d2 = new Date(obj.specialData[1].content.dateAdded);

console.log('d1 is:', d1.toLocaleString());
console.log('d2 is:', d2.toLocaleString());

console.log('d1 - d2: ', d1 - d2, 'seconds');

output

desc value
d1 26/12/2017, 23:21:37
d2 12/01/2018, 23:21:37
d1 - d2 -1468800000 seconds
user2226755
  • 12,494
  • 5
  • 50
  • 73