odata-query
odata-query copied to clipboard
Support `date` and `datetime` data type
Extracted from https://github.com/techniq/odata-query/pull/20#issuecomment-407770014
Regarding dates and times they do not appear to qualify the values with a type except optionally with a duration.
DateValue eq 2012-12-03
DateTimeOffsetValue eq 2012-12-03T07:16:23Z
DurationValue eq duration'P12DT23H59M59.999999999999S'
DurationValue eq 'P12DT23H59M59.999999999999S'
TimeOfDayValue eq 07:59:59.999
We could support the following in handleValue
-
{ type: 'datetimeoffset', value: date }
and aDate
instance (like we do now):-
date.toISOString()
=>2012-12-03T07:16:23Z
-
-
{ type: 'date', value: date }
:-
date.toISOString().split('T')[0]
=>2012-12-03
-
We should also support date
as either a Date
instance or a string (ex. 2018-01-02
):
const dateValue = (typeof date === 'string') ? new Date(date) : date instanceof Date ? date : null
const value = dateValue && dateValue.toISOString();
I think it would be nice also to accept integer, when a type is given.
let dateValue = null;
switch (typeof value) {
case 'string':
case 'number':
dateValue = new Date(value).toISOString();
break;
case 'object':
if (value instanceof Date) dateValue = value.toISOString();
break;
}