OData:日期“大于"筛选 [英] OData: Date "Greater Than" filter

查看:389
本文介绍了OData:日期“大于"筛选的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否可以通过指定大于xxxxx的日期"过滤器来返回OData中的一系列记录...但是使用以前从OData提要中获取的Date,

Is there a way to return a series of records in OData by specifying a "Date greater than xxxxx" filter...but using a Date that was previously obtained form an OData feed?

用例:假设我要构建一个显示最近完成的在线订单列表的网页.这是我的目标:

Use Case: Pretend that I want to build a web page that displays a list of the most recently completed online orders. This is what I'm aiming for:

  1. 加载页面
  2. 异步打我的OData服务,返回最近的100个订单(按日期降序排列,以便最先显示最近完成的订单)
  3. 使用OData数据在页面上构建HTML
  4. 将MAX日期存储到全局变量中(看起来像这样:/Date(1338336000000)/)
  5. 以30秒的间隔命中OData服务,但是这次指定一个过滤器以仅返回订单日期大于先前的最大日期"的记录.在这种情况下:/Date(1338336000000)/
  6. 如果返回任何记录,请为这些记录构建HTML,然后将项目放在先前加载的项目之前.

我要努力的是指定大于"日期过滤器.出于某种原因,OData中的日期过滤器似乎无法与OData自己的本机日期格式配合使用.我是否需要将原始获取的日期转换为可用于过滤的其他格式?

Where I am struggling is in specifying the Date "greater than" filter. For some reason, the date filters in OData do not seem to play very nice with OData's own native date format. Do I need to convert the date originally obtained into a different format that can be used for filtering?

我想做这样的事情:

[http://mydomain/Services/v001.svc/Orders?$filter=close_dt gt 1338336000000][1]

仅供参考:我正在使用V2

FYI: I'm using V2

推荐答案

解决了这个问题.

OData V2开箱即用以JSON Date格式返回SQL之外的日期,如下所示:

OData V2 out-of-the-box returns dates out of SQL in JSON Date format like so:

/Date(1338282808000)/

但是,为了在OData调用中使用日期作为过滤器,您的日期必须采用EDM格式,如下所示:

However, in order to use a date as a filter within an OData call, your date has to be in EDM format, looking like this:

2012-05-29T09:13:28

因此,我需要从最初的OData调用中获取日期,然后将其转换为EDM格式,以便在后续的OData调用中使用,如下所示:

So, I needed to get the date from my initial OData call, then convert it to the EDM format for use in my subsequent OData calls, which look like this:

/Services/v001.svc/Orders?$filter=close_dt gt DateTime'2012-05-29T09:13:28' 

我最终创建了一个JavaScript函数,该函数可以进行格式化切换:

I ended up creating a javascript function that does the formatting switcharoo:

function convertJSONDate(jsonDate, returnFormat) {
        var myDate = new Date(jsonDate.match(/\d+/)[0] * 1);
        myDate.add(4).hours();  //using {date.format.js} to add time to compensate for timezone offset
        return myDate.format(returnFormat); //using {date.format.js} plugin to format :: EDM FORMAT='yyyy-MM-ddTHH:mm:ss'
    }

一些注意事项:

  • JSON格式似乎无法适应时区,因此返回的日期与我在数据库中看到的日期不匹配.因此,我不得不手动添加时间来进行补偿(请对此进行解释).
  • 我正在使用 date.format.js 插件,您可以下载
  • The JSON format does not seem to adjust for timezone, so the date returned does not match the date I see in my database. So I had to add time manually to compensate (someone please explain this).
  • I am using the date.format.js plugin which you can download here for formatting the date and adding time.

这篇关于OData:日期“大于"筛选的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆