cancel
Showing results for 
Search instead for 
Did you mean: 

How to get UTC date from bound datepicker and format it

Former Member

Hello,

I am developing a Fiori app which uses a date picker, and I am having troubles getting the correct date to display and format that date. The desired format is YYYY/MM/DD.

*displayFormat property of date picker only works on newly selected dates from the date picker, not bound values coming from the backend*

The date saved in the backend is "Aug 1 2016". When it reaches my formatting function, it is "Sun Jul 31 2016 18:00:00 GMT-0600 (Mountain Daylight Time)", I understand it is 6 hours behind the actual date due to timezone offset. I can use date.toUTCString() to get the proper date, however I want to avoid parsing a string to format it the way I want. I thought that using .getUTCMonth() and .getUTCDate() and other getUTC functions would allow me to work with the same date and format it the way I want, however the month returned in getUTCMonth() is different than the month in toUTCString(), and I don't know why!

See screenshot of my debugger below for visual explanation:

The desired date should be "2016/8/1" in this case, which is correct in toUTCString, but when trying to format it using UTC methods, I get a different date, and non UTC methods return an even "more wrong" date.

Thank you for reading this, any information you can provide is greatly appreciated.

Michael

Accepted Solutions (1)

Accepted Solutions (1)

former_member227918
Active Contributor

I think you can use below code in your xml view, no need to use formatter function and this will handle timezone diff and all.

<DatePicker value="{path: 'fromDate', type: 'sap.ui.model.type.Date', formatOptions: {pattern: 'yyyy/MM/dd', strictParsing: true , UTC: true}}">
</DatePicker>

Hope will be helpful.

Regards,

Akhilesh

former_member662960
Participant

Is there a way to set the UTC value dynamically? Like taking it from a model or setting it programatically from the controller?

Answers (1)

Answers (1)

UxKjaer
Product and Topic Expert
Product and Topic Expert
0 Kudos

In your XML view you should be able to use the following in the datepicker tag

displayFormat="YYYY/MM/DD" valueFormat="YYYY/MM/DD"

That should work. Otherwise try using a formatter.

seVladimirs
Active Contributor
0 Kudos

You can also check additionaly information how to use 'sap.ui.core.format.DateFormat' use https://help.sap.com/saphelp_uiaddon10/helpdata/en/91/f2eba36f4d1014b6dd926db0e91070/content.htm