Date conversion in fiori app
2023-11-22 08:1:58 Author: blogs.sap.com(查看原文) 阅读量:5 收藏

Problem Statement:

Dates behaves differently for different timezones. When we read data from backend for some timezones it is observed that date is getting reduced by 1.

Note:  Here I know from backend service I must get data for 27th June 2022 for WORKDATE property, but

  1. UTC+05:30 timezone data read: date was read correctly.

   2. UTC-6:00 timezone data read: date was reduced by 1 day.

Solution:  code to write in controller.js file.

var oDate = oData.results[l].<dateproperty>;
oData.results[l].<dateproperty> = 
new Date(oDate.getUTCFullYear(),oDate.getUTCMonth(),oDate.getUTCDate());

Here after reading the data, convert the date using above code(so that as per the above example we will have  27th June 2022 for WORKDATE property).

Result:

the above frontend code will ensure that date will be according to the timezone, and you will not have any mismatch in date values in the data.

  • From formatter if we have to convert the incoming date: use UTC to true.
 sap.ui.core.format.DateFormat.getInstance({
style: "medium",
calendarType: sap.ui.core.CalendarType.Gregorian,
UTC: true
});

below are some of blog posts on date related stuff.

https://blogs.sap.com/2021/10/17/handling-edm.datetime-in-different-time-zone-in-ui5-application/

https://blogs.sap.com/2019/06/05/ui5-smartcontrols-how-to-use-date-and-time-related-data-types/

I hope this might be useful when you work with different timezones on dates.

If you have any questions please comment below, I will be happy to answer them.

Copyright Information: all the images attached here are created by me.

Thanks,

Chandra.


文章来源: https://blogs.sap.com/2023/11/22/date-conversion-in-fiori-app/
如有侵权请联系:admin#unsafe.sh