Home > Mobile >  convert mongodb creation date to utc 5 for a search filter, by date
convert mongodb creation date to utc 5 for a search filter, by date

Time:07-13

Good morning and thanks in advance. I tell you my problem, I am making a filter to search for requests for certain specific elements and when searching for a date in YYY-MMM-DD I had the problem. It turns out that mongo saved it to me with this format:

createdAt 2022-07-10T18:11:28.630 00:00,

updatedAt 2022-07-10T18:11:28.630 00:00

which I was able to "fix" with:

if (payload.date) {
query.createdAt = { 
$gte: new Date(`${payload.date}T00:00:00.000-00:00`), 
$lt: new Date(`${payload.date}T23:59:59.999-00:00`) 
};

but my problem arose that when saving and searching by date, it seems that I save it with timezone zero, so I consult the date 2022-07-11 and it did not bring any data, I tried with 2022-07-12 and it just returned a response of documents.

I think my solution would be to use moment and convert it to utc 5 when doing the query. Does anyone have an idea how I can do that fix? I would appreciate it very much.

CodePudding user response:

Maybe similar to this

query.createdAt = { 
   $gte: moment.tz(payload.date, "America/Toronto").startOf('day').toDate(), 
   $lte: moment.tz(payload.date, "America/Toronto").endOf('day').toDate()
};
  • Related