cancel
Showing results for 
Search instead for 
Did you mean: 

DatePicker - Wrong conversion in the date range - ToDate is increased by 1 day always

former_member184739
Contributor

Hello Experts,

I'm experiencing a weird scenario where the date range in the UI5 datepicker is not picking up the correct values. Let me summarize the steps done and what exactly the issue am facing.

I have placed the datepicker range as per the following code in the view.xml file.

<DateRangeSelection id="dateCreatedOn" placeholder="{i18n>enterCreatedOn} ..." valueFormat="MM-dd-yyyy" displayFormat="MM.dd.yyyy"/>

Date range selected in the user screen is captured in the controller.js as shown below.

if (dateCreatedOnFrom && dateCreatedOnTo) {
  dateCreatedOnFrom = new Date(dateCreatedOnFrom);
  dateCreatedOnTo = new Date(dateCreatedOnTo);
 this.inputFilters.push(new Filter("CreatedOn", FilterOperator.BT, dateCreatedOnFrom, dateCreatedOnTo));
			}

As you can notice when debugging this in Chrome, date range values i.e., both from and to are fine.

Date ranges selected in the UI5 application:

Values in Google Chrome console:

But when the odata call is triggered to SAP Gateway 'dateCreatedOnTo' value is changed i.e., increased by 1 day when the odata call is triggered.

/SearchResultSet?sap-client=110&$filter=CreatedBy eq 'SUZIP' and (CreatedOn ge datetime'2020-09-21T04:00:00' and CreatedOn le datetime'2020-09-25T03:59:59.999')

When call reached the backend i.e., ECC, we can notice the changed date values which leads to pull wrong data.

Note:

SAPUI5 version: 1.71.4 is deployed in the Gateway FrontEnd server.

Please shed some lights on this issue. Thank you for the support.

Regards

Praba

View Entire Topic
former_member184739
Contributor

Hi Folks,

Below code snippet was added to send the correct date to SAP backend after formatting. I hope this helps.

	var dateCreatedOnFrom = oView.byId("dateCreatedOn").getDateValue().
	var dateCreatedOnTo = oView.byId("dateCreatedOn").getSecondDateValue().
	
if (dateCreatedOnFrom && dateCreatedOnTo) {
	//Format date to remove UTC issue     
	 var oFormatDate = sap.ui.core.format.DateFormat.getDateTimeInstance({
	     			pattern: "yyyy-MM-ddTKK:mm:ss"
	});
	//Convert DateTime into Date to avoid TimeZone Issue 
	var oDateFrom = oFormatDate.format(dateCreatedOnFrom); 
	    oDateFrom = oDateFrom.split("T");
	    dateCreatedOnFrom = oDateFrom[0];
				
	var oDateTo = oFormatDate.format(dateCreatedOnTo); 
	    oDateTo = oDateTo.split("T");
	    dateCreatedOnTo = oDateTo[0];				
       this.inputFilters.push(new Filter("CreatedOn", FilterOperator.BT, dateCreatedOnFrom, dateCreatedOnTo));
			}

Regards

Praba

sergei-u-niq
Active Contributor

maybe a small improvement to your code:

new Date( dateCreatedOnFrom.getTime() - dateCreatedOnFrom.getTimezoneOffset() * 60 * 1000 )

(this way you don't need to work with formatters or rely on string representations - just work with the date object)

rauf_shaikh
Active Participant
0 Kudos

Thanks for sharing this knowledge, it helped me.

-Regards,

Rauf