0

So I have a date being pulled from a database called preCountDate

for this example preCountDate = 2017-10-12

I need to check if this is bigger than todays date which I am setting like this preToday = new Date(); now I can see that they are returning different formats which I assume to the issue and why this isn't working:

if(preCountDate > preToday){
    preToday = [preCountDate, preCountDate = preToday][0];
    preCountDate = moment(preCountDate).subtract(1, 'day');
}

I use the above to check is the preCountDate is bigger, if yes then swap them around.

This doesn't get run with the above. I think its to do with the format that preToday returns but not sure how to format in a way that will work

help appreciated

Different to the one suggested as I am not using unix time

1

2 Answers 2

1

Try this

preCountDate = new Date(preCountDate); // this sorts out the format problem
if (preCountDate > preToday){
    // whatever you want to do with the dates here
}
Sign up to request clarification or add additional context in comments.

Comments

1

If you are using moment you could try this:

var preCountDate = moment("2017-10-12");
var preToday = moment();

if (preCountDate > preToday) {
   // do your thing...
}

or

if (preCountDate.isAfter(preCountDate)) {
   // do your thing...
}

or

if (preCountDate.diff(preCountDate) > 0) {
   // do your thing...
}

3 Comments

This is probably a better answer than mine
There is no better answer than other... there are just different ways to do the same thing.
That's very gracious. But given that the OP was using moment anyway, it does make a bit more sense to use moment throughout. Either way, at least he's got it working now.

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.