Comparing dates after saving with Mongoose

Here's part of my mongoose schema

var mySchema = mongoose.Schema({
  expires: { type: Date, required: false }
}

I insert a document, set to expire tomorrow:

item.expires = Date.now() + 1;
item.save(...);

Then I read save and read the document and test to see if it's expired

if (item.expires < Date.now())

However this expression always returns true. Looking at my debugger, item.expires is listed as Date whilst Date.now() is listed as a long number.

How can I do this test and why are the two different types even thought they are set the same way?

Answers


Date.now() returns a number, an unix timestamp. It's in milliseconds, so you are not setting the expire date to the next day but the next millisecond. Said that, if you to transform the number to a Date object again you will need to use Date:

 item.expires =          Date.now() + 24*3600*1000;
 //or
 item.expires = new Date(Date.now() + 24*3600*1000);

You add 24 hours of milliseconds and then, if you want, transforms it to a Date object again.


Need Your Help

AttributeError: form instance has no attribute 'buttonI' in pygtk

python python-2.7 gtk pygtk attributeerror

I'm trying to write a script that will do check-box invisible when i click on main check-box.

How to convince chrome(browser) to show the download file dialog?

wcf google-chrome browser mime-types

How to convince chrome(browser) to show the download file dialog while downloading a JSON content?