typescript - 如何查询ISO日期为今天的对象?

标签 typescript elasticsearch firebase-realtime-database

我有以下数据:

[
  ...
  {
      date: {
        startDate: '2017-04-21T00:00:00.000Z',
        endDate: '2017-04-21T00:00:00.000Z',
        startTime: '12:00',
        endTime: '21:00'
      }
    },
    {
      date: {
        startDate: '2017-04-21T00:00:00.000Z',
        endDate: '2017-04-21T00:00:00.000Z',
        startTime: '09:00',
        endTime: '23:00'
      }
    }
  ...
]

这是我到目前为止所拥有的:
export const OngoingEventsQuery: any = {
  type: 'event',
  body: {
    from: 0,
    size: 2,
    query: {
      range: {
        'date.startDate': {
          gt: new Date().getDate() - 1
        },
        'date.endDate': {
          lt: new Date().getDate() + 1
        }
      }
    }
  }
};

现在,我要做的是匹配与今天的日期匹配的对象,并考虑startTimeendDate。因此,如果今天的日期是april 21 2017而时间是15:00,则上述两个对象都应匹配。

首先尝试在不匹配开始时间和结束时间的情况下,此查询为我提供了数据库中所有可能的对象,这确实是错误的。

我以为我必须给yesterdaytomorrow之间的一个日期,以便通过使用带有加号1或减号的today来获得getDate()。但这显然是错误的。

我怎样才能做到这一点?

编辑:奇怪的是,如果我从查询中删除fromsize,我将获得0个结果。

最佳答案

{
  "query": {
    "bool": {
      "must": [
        {
          "range": {
            "date.startDate": {
              "gte": "now/d",
              "lte": "now/d"
            }
          }
        },
        {
          "range": {
            "date.endDate": {
              "gte": "now/d",
              "lte": "now/d"
            }
          }
        }
      ]
    }
  }
}

the documentation中,日期内部保存在UTC中,因此,如果您的比较将使用UTC时间,则应该进行以下操作:

Internally, dates are converted to UTC (if the time-zone is specified) and stored as a long number representing milliseconds-since-the-epoch.

关于typescript - 如何查询ISO日期为今天的对象?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43540165/

相关文章:

typescript - "Type ' () => string[] ' is missing the following properties from type ' string[] ': pop, push, concat, join, and 25 more" typescript 中出现错误

ubuntu - Fluentd、Elasticsearch 和 Kibana 设置

javascript - Firebase 云功能更新并非每次发生写入时都有效

javascript - 解除 ng-repeats 内 ng-model 的绑定(bind)

javascript - Angular 2 组件工厂解析器辅助函数

angular - 在 ngFor 循环中动态地向元素添加一个类

javascript - 如何区分 blob 和 String?

Elasticsearch 7.4 错误地提示快照已经在运行

elasticsearch - 如何计算Elasticsearch字段大小

swift - 如果 Firebase 快照为零,则 UITableView Cell 的默认值