V
V
Vann Damm2020-12-19 20:45:51
JavaScript
Vann Damm, 2020-12-19 20:45:51

Getting data incorrectly, depending on the date?

When receiving data, depending on the date, the data is displayed incorrectly. I do it this way.
Get date:

const date = new Date();
date.setDate(date.getDate());

The query itself: I need to find all data added today. It doesn't find anything.
const tasks = await TaskModel.find({
        userId: `${req.user.id}`,
        time: { $eq: dates },
      }).exec();

We have to redo the crutch: Subtract one from the date. And the search is to do "Greater than or equal to." So it finds .... which sounds crazy. But I can't imagine why it doesn't work with $eq ..
This is how the crutch works:
const date = new Date();
date.setDate(date.getDate() - 1);

const tasks = await TaskModel.find({
        userId: `${req.user.id}`,
        time: { $gte: dates },
      }).exec();

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
Aetae, 2020-12-19
@effect_tw

date.setDate(date.getDate());this line makes no sense (does nothing).
If you want to cut off the time of the date (and in fact put 0 hours 0 minutes, etc.), then this is explicitly divided: . If the date is needed in the standard time zone, then use , after which instead of - . Well, on your server, dates should be stored without time, otherwise no , but only + . date.setHours(0, 0, 0, 0)
eqgtelte

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question