【问题标题】:Realm Javascript filtered on Date but ignore yearRealm Javascript 过滤日期但忽略年份
【发布时间】:2020-08-25 22:28:48
【问题描述】:

我正在尝试在 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

【问题讨论】:

    标签: realm realm-js


    【解决方案1】:

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

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

    然后在Student中使用:

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

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2015-09-20
      • 2011-12-24
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2010-10-10
      • 1970-01-01
      • 2021-11-01
      相关资源
      最近更新 更多