Hi,
OData does support filtering by date and time, but the correct syntax can be a bit finicky. You must ensure that the datetime value is formatted according to the OData version that the API expects. Generally, in OData v4, you don't need the datetime keyword anymore.
If you're encountering issues, it's possible there might be a problem with the syntax or the API you are working with might not fully support the OData datetime filter.
Your error message seems to indicate that the datetime literal is not being recognized. In OData v4, the correct format for a datetime filter in a URL should look like this:
...&$filter=CreatedDate gt 2023-10-30T10:23:56Z
Here's a simplified step you might try:
- Ensure that the API you're using supports the OData datetime filter.
- Confirm whether the API uses OData v4 or an earlier version, as the syntax differs.
- Correct the syntax of the datetime filter according to the version of OData.
For an OData v4 filter, you can try formatting your variable like this:
formatDateTime(addDays(utcNow(), -7), 'yyyy-MM-ddTHH:mm:ssZ')
And then in your OData filter:
$filter=CreatedDate gt 2023-10-30T10:23:56Z
If the API requires single quotes around the datetime, make sure they're properly included:
$filter=CreatedDate gt '2023-10-30T10:23:56Z'
Be mindful of the time zone information (Z indicates Zulu/UTC time). If the API expects the local time, you'll need to adjust the time zone accordingly. Also, note the use of HH for 24-hour time formatting, which could be a potential issue if hh is used instead.
If these steps don't resolve the issue, you might need to consult the API documentation or reach out to the API provider for more specific guidance on the correct datetime filter syntax for their service.
Best Regards,
Hassan Raza