1

currently, I'm trying to add some date parameter to my add

from my backend I get

/Date(1525521600000+0000)/

I tried some methods like

    <Text text="{path: 'ValueDate',
                type: 'sap.ui.model.odata.type.DateTime',
                formatOptions: {
                style: 'medium'                                                         
                       }
                }"/>

But this gives me no Output

If i try

<Text text="{path: 'ValueDate',
                type: 'sap.ui.model.odata.type.Date',
                formatOptions: {
                style: 'medium'                                                         
                       }
                }"/>

I simply get out

enter image description here

But I want an Output like dd/MM/yyyy

Penrock
  • 143
  • 1
  • 3
  • 16

2 Answers2

2

Binding:

 text="{
        path: 'ValueDate',
        formatter: '.formatter.dateFormatter'
    }"

Formatter Function

 function dateFormatter (jsonDateString){ 
   return new Date(parseInt(jsonDateString.replace('/Date(', '')));
}
dotchuZ
  • 2,621
  • 11
  • 39
  • 65
1

If you don't get a standard OData Date Object, you have to do this via the formatter function of SAPUI5:

text="{
    path: 'ValueDate',
    formatter: '.formatter.date'
}"

in the formatter.js file you have to implement a "date" function and convert it to your desired value. For more information on formatters: https://openui5.hana.ondemand.com/1.38.5/#docs/guide/0f8626ed7b7542ffaa44601828db20de.html

Thomas L.
  • 249
  • 2
  • 15
  • isn't there a way to do this without using a formatter? – Penrock May 09 '18 at 09:20
  • No, because "/Date(1525521600000+0000)/" is not a standard OData Date or Datetime. For OData it is just a string. Please refer to this: http://www.odata.org/documentation/odata-version-2-0/overview/#AbstractTypeSystem – Thomas L. May 09 '18 at 09:23
  • I have referred to this document I found, but well it doesn't work for me https://www.sap.com/documents/2016/06/98304518-767c-0010-82c7-eda71af511fa.html – Penrock May 09 '18 at 09:36
  • 1
    why dont you send it as DateTime or Date from your Backend? would be the easiest imho | and btw. the formatter should also work for a string... ;-) so where's the issue? – dotchuZ May 09 '18 at 10:08
  • @zYrEx I tried Dats as type from my backend too, but got the same result – Penrock May 09 '18 at 11:55
  • do you have a working formatter on this particular field? paste your function, then I'll add some coding... – dotchuZ May 09 '18 at 11:56
  • to be honest, I don't really know how to write this particular formatter. – Penrock May 09 '18 at 12:03
  • then you should invest into this, formatter is one of the basic functions, necessary all over ui5 apps .... if you found it out use: function dateFormatter (jsonDateString){ return new Date(parseInt(jsonDateString.replace('/Date(', '')));} and chk out: https://stackoverflow.com/questions/1244094/converting-json-results-to-a-date – dotchuZ May 09 '18 at 12:32
  • Formatters: https://openui5.hana.ondemand.com/1.38.5/#docs/guide/0f8626ed7b7542ffaa44601828db20de.html – dotchuZ May 09 '18 at 12:32
  • @zYrEx Thank you for your advice, I did know how to use a formatter, my problem was, that I didn't know what I had to use in the {} bracket, which you solved for me :) – Penrock May 14 '18 at 08:00
  • ok, glad to help. nicely accept one of the answers and upvote the other one, because both are correct in some way. – dotchuZ May 14 '18 at 08:30