0

我正在尝试在 RealmJS 中查询生日的日期字段。

我想找到所有今天过生日的人。

问题是我不能忽略这一年。有没有办法做到这一点?

我的代码:

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);

即使今天有学生过生日,它也没有显示,因为它正在寻找 2020 年。

我怎么能忽略年份?

这是 student.birthday 在数据库中的格式,它是一个领域日期: 2000-08-25T10:00:00.000Z

4

1 回答 1

0

您无法通过date属性轻松解决此问题。我建议你创建一个生日对象模式:

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

然后在Student

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

您现在可以过滤Student和。birthday.monthbirthday.day

于 2020-11-25T20:52:48.110 回答