0

I'm trying to format a JSON date to a JavaScript date to display it in a nice way. The original date comes from a JSON-object, which looks like this:

{
    "name": "foo",
    "num": "1",
    "date": "\/Date(1367539200000)\/"
}

The place where the JSON elements should be displayed later is a SAPUI5 object header:

objectHeader = new sap.m.ObjectHeader({
    title: "{/name}",
    number: "{/num}",
    attributes: [
        new sap.m.ObjectAttribute({
            text: "{/date}"
        })
    ]
 });

Since the JSON object is bound to the object header via

dataModel.setData(json)
objectHeader.setModel(dataModel)

the values are correctly substituted. But i want to have the date correctly formatted to a more readable format instead of seeing /Date(1367539200000)/ on my website. I tried with

new sap.m.ObjectAttribute({
    text: new Date(parseInt("{/date}".substr(6))).toLocaleString('de');
})

But that failed with an 'Invalid Date'. What would be the right syntax to format the JSON date to a Javascript data object in a model binding?

c7n
  • 1,131
  • 16
  • 29
  • Does this answer your question? [How to Add Date / Time from OData Service Correctly to UI?](https://stackoverflow.com/questions/47593990/how-to-add-date-time-from-odata-service-correctly-to-ui) – Boghyon Hoffmann Jul 12 '21 at 15:37

1 Answers1

1

You can use a formatter to do that for you. The advantage is that you can properly use databinding, so your controls will be updated automatically in case the model changes.

new sap.m.ObjectAttribute({
    text: {
        parts: [
            {path: "/date"}
        ],
        formatter: function(date){
            //do whatever you want
            return /* the value you want to have as result */;
        }
    }
})
herrlock
  • 1,454
  • 1
  • 13
  • 16
  • your idea seems like the solution i was looking for! But when I try it, `date` in the formatter function is `null` or `undefined` means `date.substr(6)` doesnt work.. any ideas on that? – c7n Jan 16 '15 at 16:46