0

I have the following:

DATE_FORMAT=YYYY-MM-DD HH:mm:ss
const differenceInDays = require('date-fns/difference_in_days');

const newDateNow = () => {
    const date = new Date();
    return formatDate(date, process.env.DATE_FORMAT);
};

const isValid = (expdate) => {
  today = newDateNow();
  differenceInDays(expdate, today) >= 0;
};

Now in a method I have:

const c = isValid(a);

this returns false, where I would expect true. Logging the variables I have:

today = 2019-12-12 17:55:48
expdate = 2020-10-09T22:00:00.000Z

So they have different date formats, which might be the reason isValid returns valid even though it should be true (given that a is further into the future than date).

How can I make this work? Should I perhaps transform the date format of a to something similar of date? (but how to do that?)

Nick
  • 3,496
  • 7
  • 42
  • 96
  • Maybe this library can help you: https://momentjs.com/docs/ – probitaille Dec 12 '19 at 17:18
  • Also, your question may be related to this one: https://stackoverflow.com/questions/22600856/moment-js-date-time-comparison/22601120 – probitaille Dec 12 '19 at 17:20
  • Why don't you work with the `Date` objects without formatting them to strings? Date objects can be compared easily with the `<` and `>` operators... It is only when you need to display a date that you should worry about formatting. – trincot Dec 12 '19 at 17:26

0 Answers0