0

I am attempting to query in RealmJS on a Date field for birthdays.

I want to find all the people that have a birthday today.

The problem is that I can't ignore the year. Is there a way to do this?

My code:

const today = new Date();
const morning = new Date(today.getFullYear(), today.getMonth(), today.getDate(), 0,0,0,0);
const evening = new Date(today.getFullYear(), today.getMonth(), today.getDate()+1, 0,0,0,0);
const studentBirthdaysToday = realm.objects('Student').filtered('birthday >= $0 && birthday < $1', morning, evening);

Even though there are students with a birthday today, it doesn't show because it's looking for the year 2020.

How can I ignore the year?

This is how student.birthday is formatted in the database, it's a Realm Date: 2000-08-25T10:00:00.000Z

Kurt
  • 3
  • 2

1 Answers1

0

You cannot solve this problem easily with a date property. I suggest you create a birthday object schema:

const Birthday = {
  name: "Birthday",
  properties: {
    year: "int",
    month: "int",
    day: "int"
  }
};

and then use it in Student:

const Student = {
  name: "Student",
  properties: {
    // ...
    birthday: "Birthday"
  }
};

You can now filter Student on birthday.month and birthday.day.

geisshirt
  • 2,457
  • 1
  • 17
  • 20