SAPUI5 oData – Date format to oData – Post and Get

Some tips to show quickly, how to format a Date value from, and to oData. In this example we are using a Hana Cloud database table.

The table has the following field date, named birthday:

When we execute the method GET in JSON format, we receive it in this way:

    "id": 1,
    "name": "Homer",               
    "last_name": "Simpson",                
    "gender": null,                
    "birthday": "/Date(-14739840000)/",                
    "salary": "1500"

The value “birthday” is the date in milliseconds since Unix Epoch. 

You can find more information here: http://en.wikipedia.org/wiki/Unix_time

So, if you try to read, or to execute a POST method without any format, it won’t work appropriately. You can get errors like “Syntax error at position 0.”.

To solve this in XML view, where you are getting the data from oData, you can use an Input field like below. No special need in the controller to get the data.

<Input id="birthday"  value="{
    path: 'modelHanaDev>birthday',
    type: 'sap.ui.model.type.Date',
    formatOptions: { 
    		pattern: 'dd/MM/yyyy',
    		UTC:true
    }
  }" />

To solve this format problem when you are trying to POST (create new record in the table) the value, you can use the example below in the java script function:

// Format value in XML view valueFormat="yyyy/MM/dd"
var valueInput      = this.getView().byId("birthday").getValue();
var birthday        = new Date(valueInput); 
newEntry.birthday	= birthday;

Example of XML view field, where is the new date to post:

<DatePicker id="birthday" 
		displayFormat="dd/MM/yyyy" 
		valueFormat="yyyy/MM/dd" 
		placeholder="14.02.1989" />

Regards

Leave a Reply

Your email address will not be published. Required fields are marked *