viji kumar
viji kumar

Reputation: 11

date formatting in sapui5

I have this date coming in the json- startDate: "2014-12-07T00:00:00" and the view should render this as- Dec 7, 2014. and I used the follwoing to format it.

text="{ path: 'startDate',
        type: 'sap.ui.model.type.Date',
        formatOptions: {
                source: {pattern: 'yyyy/MM/dd HH:mm:ss'}, 
                style: 'medium'
       }}"

I am not clear how this date formatting works. I mean what is the pattern that needs to be given? Please help me with this. thanks !

Upvotes: 1

Views: 13918

Answers (3)

Gurdev
Gurdev

Reputation: 1

Given your startDate: "2014-12-07T00:00:00", I would suggest using the source pattern as mentioned in the snippet below (yyyy-MM-ddTHH:mm:ss) as it matches your source date.

And instead of using 'style' attribute, go the old fashion way. So, in your case, the pattern 'MMM dd, yyyy' should do the job.

<Text text="{path : 'startDate', 
type:'sap.ui.model.type.Date', 
formatOptions: 
 { 
 source : 
  {
  pattern: 'yyyy-MM-ddTHH:mm:ss' 
  },
 pattern: 'MMM dd, yyyy'
 }
} 
"/>

For modifications in the pattern, I would recommend using the date format abbreviations mentioned here

Upvotes: 0

zypA13510
zypA13510

Reputation: 1262

I also had this problem of parsing JSON formatted dates. In my case the string is like

JSON.stringify(new Date(Date.now()));
// returns "2018-03-06T05:20:16.143Z"
new Date(Date.now()).toISOString();
// returns 2018-03-06T05:20:16.143Z

which according to MDN has been standard in ECMAScript 5.1 (ECMA-262). The correct pattern to use in this case is yyyy-MM-ddTHH:mm:ss.SSSX.

<Text text="{ path: 'myDateTime',
    type: 'sap.ui.model.type.DateTime',
    formatOptions: {
        source: {pattern: 'yyyy-MM-ddTHH:mm:ss.SSSX'},
        style: 'medium'
    }}"/>

Also, if your JSON uses a different notation, you can build your own pattern according to this: Date Field Symbol Table

I hope this can help anyone who comes across this dealing with JSON formatted DateTime value.

Upvotes: 1

viji kumar
viji kumar

Reputation: 11

it worked when i made the pattern to this yyyy-MM-ddTHH:mm:ss

Upvotes: 0

Related Questions