cancel
Showing results for 
Search instead for 
Did you mean: 

OData working with dates and times

cmetz
Participant
0 Kudos

Hey guys,

i have a issue regarding handling dates and times with OData. I need to convert the data from odata to show it in the right format at the frontend. I worked through this post already:

I could get it worked with dates, but times are still not working.

The problem is somehow the conversion from OData to Javascript objects. The block says:

"Luckily if you use the ODataModel, datajs the OData library SAPUI5 uses has regular expressions and code logic which parses these values into JavaScript for us. Below is how this Entity looks parsed to JavaScript."


How is that working? Can someone please explain how I can see the Odata in JavaScript Objects?

Here is what I get when I call the Service? And there is also the problem. I have no ms in the time and therefore I cant convert it into a new date.

Thanks + regards

Chris

Accepted Solutions (0)

Answers (1)

Answers (1)

Former Member
0 Kudos

Hi Christoph,

Suppose you are exposing this date in a define column table or textfield. You can manipulate the format by using pattern.

cmetz
Participant
0 Kudos

Thanks for your reply, Carlos.

I am using XML view. How can I apply this to a XML view. At the moment I am using a formatter.

<ObjectStatus  text="{path: 'ModelKundenInfos>UhrzeitAnlieferung',

      formatter: 'sap.ui.demo.myFiori.MeineRouteApp.util.Formatter.time'}"

></ObjectStatus>

Here is my time function in the formatter:

time : function(value) { //"PT12H25M36S"

  if (value) {

  debugger;

  var oTimeFormat  = sap.ui.core.format.DateFormat.getTimeInstance({pattern: "HH:mm"});

  var oNewDate = new Date(value); //Invalid date

  var oFormatedDate = oTimeFormat.format(oNewDate);

  return oFormatedDate;

  } else {

  return value;

  };

  },

If i create a new Date with new Date(PT12H25M36S) i get a invalid date. What the did in the blog I mentioned above is, that the passed the milliseconds to create a new date. But I dont have the milliseconds. I have been searching for ages already.

Maybe there is an easier way with your approach Carlos. Do u have any ideas how to do your approch with an XML-View.

Thanks Chris

Former Member
0 Kudos

Hi Christoph,

I will be honest with you managing dates has been one hell of a challenge at the moment and I still have a lot of problems too.

The sap.ui.core.format.DateFormat.getTimeInstance you are using uses pattern from LDML. I would have a look in all available patter and try and error.


In regards the Odata model and why it parses to js. When you declare the OData model as of my understanding you can switch on or off using true or false at the end.


new sap.ui.model.odata.ODataModel("/<name>/<name2>/<name3/name.xsodata/", true); // or you could use false.



cmetz
Participant
0 Kudos

Thanks Carlos its working now. The solution was the boolean flag, I had to set it to true and in my code I had to add value.ms:

var oNewDate = new Date(value.ms);


But what option did I set exactly (which parameter)?


new sap.ui.model.odata.ODataModel



(sServiceUrl, bJSON?, sUser?, sPassword?, mHeaders?, bTokenHandling?, bWithCredentials?, bLoadMetadataAsync?, annotationURI?, loadAnnotationsJoined?)


And is it possible to get this odata javascript data (as shown in the blog -> screenshot) somehow within the browser?


Chris

Former Member
0 Kudos

Hi Christoph,

I am not sure if you are familiar with Firefox and Google chrome developer tools. This tools are particularly good for tracking the request communication and what is been sent back and forward. You can even edit the values and resend.

To show the metadata of your Odata model you just need to type service path in your internet browser and at the end you can type some of the below extensions.

<path>/<sevice name>.xsodata/$metadata

<path>/<sevice name>.xsodata/<odataname>/?$format=json

<path>/<sevice name>.xsodata/<odataname>/?$top=3

<path>/<sevice name>.xsodata/<odataname>/?$top=3&skip=1

This is how you would test the model before start building a user interface on it. That is what I have been doing at list.

cmetz
Participant
0 Kudos

Thanks Carlos for your reply. Yes I know how to test the OData Model in the Browser, but I mean the following view. I cant get this view:

And which parameter did I set in the oModel excactly to true -> new sap.ui.model.odata.ODataModel("/<name>/<name2>/<name3/name.xsodata/", true);


I dont know how to determine this.

(sServiceUrl, bJSON?, sUser?, sPassword?, mHeaders?, bTokenHandling?,bWithCredentials?, bLoadMetadataAsync?, annotationURI?,loadAnnotationsJoined?)