Problem with filtering and date format

Oct 7, 2011 at 11:26 AM
Edited Oct 7, 2011 at 12:17 PM

Hi guys,

I have a problem regarding dates...
As datajs formats the date of the odata service to a JS date object I display this date object in a UI control.

Now if a user wants to filter this date he normally enters the date in a different format than
  the expected Edm.DateTime format by the odata service.

The problem is now to convert the user entry back to EDM.DateTime so I can trigger filter requests like

http://services.odata.org/Northwind/Northwind.svc/Orders?$filter=((ShippedDate%20gt%20datetime%271996-07-16T00:00:00%27))

to the server.

I have seen that datajs already provides methods like formatDateTimeOffset in odata-utils
which could be reused for that.

But this method can be used internally only...
Are there plans to expose these utils methods or otherwise will datajs provide own filter functions which accept JS date objects or strings and will do the parsing itself?

Or what can I do now to solve my problem without duplicating your code? ;)


THX

 

PS: Also I guess other util methods might be very useful ;)

Oct 7, 2011 at 5:40 PM

Hi Joe,

   Hope you are doing great.  Thanks for bringing this to our attention and we will certainly consider opening up some the utility methods in future versions of the library.  Other than the date formatting functions which other utilities you consider useful? 

For the time being, if you don't want to duplicate the code... which I completely understand :)  you can use the following hacky method. The jsonHandler can serialize a date value in the way you need it.  The following function would do the trick:

function formatDate(date) {
      var r = { headers: {}, data: date };
      OData.jsonHandler.write(r);
      return r.body;
  }

  alert(formatDate(new Date()));

I definetly rather you duplicating the code than resorting to a hack like the one above :).

Regards,

Alex Trigo.

Oct 11, 2011 at 11:59 AM

Hi Alex,

thanks for the hack, it worked just fine ;)

What would be useful in the utils are functions for the date time, date time offset parsing and formatting etc. and also perhaps the things to read the metadata like lookupEntityType etc...

This could be useful if you need information about constraints of a property etc and also for validation purposes.

Thanks!

 

Best regards

Joe