-2

I have a date stored in a database as a string. It looks like this: Tue Aug 23 2016 00:00:00 GMT-0500 (CDT)

I basically want to tell if today's date is before or after the date in the database.

The code below should sort of explain what I want. The problem is that after the difference variable doesn't return a number variable, which is what I need.

var expire = value.vaccines;
var today = new Date();
var difference = today-expire;

if(difference <= 0){
    $(element).css({"color": "#0040ff"});
}

Any ideas on how to subtract these two dates and get a number value?

Kerem Baydoğan
  • 10,475
  • 1
  • 43
  • 50

2 Answers2

0

You can just you can calculate the difference between the two Date objects and get the absolute value with Math.abs():

var today = new Date(),
    expire = value.vaccines,
    difference = Math.abs(today - expire); // difference in milliseconds

if (difference <= 0) {
    $(element).css({
        "color": "#0040ff"
    });
}

Check that expire is a valid Date object.

Yosvel Quintero
  • 18,669
  • 5
  • 37
  • 46
0

Assuming both your objects are Date

Although you only require the returned value in milliseconds, I've added the extra step of formatting the value.

Math.floor((today - expire) / (1000*60*60*24))

Taken from Here

Community
  • 1
  • 1
IronAces
  • 1,857
  • 1
  • 27
  • 36