0

I have a component that sets the startDate and endDate based on various selectors in the dropdown the user may choose.
Code looks like so:

  const handleDateOptionChange = option => {
    if (option?.value === "day") {
      setStartDate(moment().startOf("day"));
      setEndDate(moment());
    }

    if (option?.value === "week") {
      setStartDate(moment().startOf("week"));
      setEndDate(moment());
    }

    if (option?.value === "month") {
      setStartDate(moment().startOf("month"));
      setEndDate(moment());
    }

...

    setDateOption(option);
  };

For my purposes, I'm trying to compare the startDate and endDate's values to moment() objects so that I can find a way to keep the correct selector in the dropdown selected on a page refresh (I'm grabbing start and endDate from queryParams).

I have a piece of code to test this out that looks like so:

 const setDateOption = () => {
    console.log(startDate);
    console.log(moment().startOf("day"));
    if (startDate == moment().startOf("day") && endDate == moment()) {
      return dateOptions[0];
    }
    return dateOptions[1];
  };

Obviously I'm learning I cannot compare them that way. The output of those respective objects are as follows:
logs

The _d portions of the moment object match, but I've been told to not use those for comparisons. How might I compare my startDate with an actual moment() type object?

kastaplastislife
  • 293
  • 2
  • 16
  • Maybe this helps: https://stackoverflow.com/questions/22600856/moment-js-date-time-comparison/22601120 – Damian Feb 23 '21 at 18:07
  • @Damian-TeodorBeleș check my answer I just posted. Is this a preferred method of comparisons in my use case? – kastaplastislife Feb 23 '21 at 18:10
  • Does this answer your question? [Moment js date time comparison](https://stackoverflow.com/questions/22600856/moment-js-date-time-comparison) – Heretic Monkey Feb 23 '21 at 18:30

2 Answers2

0

I may have just answered my own question. Adding .format() works, like this:

 console.log(startDate.format());
 console.log(moment().startOf("day").format());

And returns an output like this:
logs

Bhargav Rao
  • 50,140
  • 28
  • 121
  • 140
kastaplastislife
  • 293
  • 2
  • 16
  • You should not expect questions-in-answers to be answered. Answers are for, well, answers to the question. This question has been asked and answered many times over. Converting dates to strings and comparing them is certainly *a* way of comparing them. Preferred? I would not say so. Dates have a native value representing the number of milliseconds since 1970-01-01T00:00:00.000 and comparing numbers is a much more well-known and easier method than comparing strings. – Heretic Monkey Feb 23 '21 at 18:34
  • My bad. I should have just edited the original post. Sorry about that. – kastaplastislife Feb 23 '21 at 18:36
0

If you are using moment version higher then 2.0, you can use the isSame function:

https://momentjs.com/docs/#/query/is-same/

Vlatko Vlahek
  • 1,839
  • 15
  • 20