Search a specific date in my collection

I want to search a specific date in my collection, I use a inputtext to enter a date with this format
DD / MM / YYYY

my collection data::
Name of the player: you you
Registration Date: Tue Jul December 2016 2:55:07 p.m. GMT + 0000 (Morocco)
Name of the player: you you
Registration Date: Wed Jul 13, 2016 3:25:47 p.m. GMT + 0000 (Morocco)

  1. npm install --save moment

  2. import moment from ‘moment’ (at the top of your client side file)

  3. convert the ‘dd/mm/yyyy’ input into a js Date object -> new Date(‘dd/mm/yyyy’)

  4. create variables for the beginning and end of that date:
    let startOfDay = moment(myDate).startOf(‘day’).toDate()
    let endOfDay = moment(myDate).endOf(‘day’).toDate()

  5. Collection.find({registrationDate: {$gte: startOfDay, $lte: endOfDay}}) … if you published the data you want to the client already then do this on the client, if not then this is the cursor that’s returned from your publish that you pass startOfDay and endOfDay to as arguments

1 Like

Two ways to do this. The first, search in a date range.

import moment from 'moment';

export function findUsersRegisteredOnDate(day, month, year) {
  const date = moment.utc().set('day', day).set('month', month).set('year', year);

  const start = moment.utc(date).startOf('day').toDate();
  const end = moment.utc(date).endOf('day').toDate();

  return Meteor.users.find({
    'registeredAt': {
      $gte: start,
      $lt: end
    }
  })
}

Alternatively, you can use date aggregation to unwind their registration date to whatever format you want.

1 Like

Thank you for your response, they are very helpful