Lewis Smith
Lewis Smith

Reputation: 1345

Javascript compare 2 differently foramtted dates

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

Upvotes: 0

Views: 81

Answers (2)

assembler
assembler

Reputation: 3300

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...
}

Upvotes: 1

Chris Lear
Chris Lear

Reputation: 6742

Try this

preCountDate = new Date(preCountDate); // this sorts out the format problem
if (preCountDate > preToday){
    // whatever you want to do with the dates here
}

Upvotes: 1

Related Questions