user13977334
user13977334

Reputation:

TypeError: yesterday.setHours is not a function

I'm trying to query results from today and yesterday, however when I try to change the time to get the full two days I get the following error

TypeError: yesterday.setHours is not a function

This is my function

exports.getReport = async (req ,res) => {
    const date = new Date();
    let today = date.setDate(date.getDate());
    let yesterday = date.setDate(date.getDate() - 1);
    yesterday.setHours(0,0,0,0)
    today.setHours(23,59,59,999)
    
    const items = await Item.find({created: {$gte: yesterday, $lte: today}});
 
    res.json({items})
} 

Any ideas?

Upvotes: 0

Views: 1171

Answers (1)

Abdelrahman Hussien
Abdelrahman Hussien

Reputation: 505

const date = new Date();
    let today = new Date(date.setDate(date.getDate()));
    let yesterday = new Date(date.setDate(date.getDate() - 1));
    yesterday.setHours(0,0,0,0)
    today.setHours(23,59,59,999)

find setDate return a timpestamp so typeof yesterday will be a number not a datetime check the above snippets

Upvotes: 1

Related Questions