cancel
Showing results for 
Search instead for 
Did you mean: 

Date format issue in Date picker in SAP UI5

DhruvKumarSingh
Explorer
0 Kudos

Hi Folks,

I have written a code which is working fine for one date picker but not working for the other date picker. I know the issue is relate to the format of the date but don't know how to fix it can anyone please help.

The code for this is here

Code For View<VBox class="sDateVbox"> <Label text="Start Date" required="true"/> <DatePicker id="datePicker" value="{oMainModel>/startDateSetupModel}" displayFormat="MM yyyy" valueFormat="dd-MM-yyyy" change="_startDateChange"/> </VBox> <VBox class="eDateVbox"> <Label text="End Date" required="true"/> <DatePicker value="{oMainModel>/endDateSetupModel}" displayFormat="MM yyyy" valueFormat="dd-MM-yyyy" change="_endDateChange" minDate="{oMainModel>/minEndDate}"/> </VBox>Code of the controller:-_startDateChange: function (oEvent) { var year = parseInt(oEvent.mParameters.value.split("-")[2]); var month = parseInt(oEvent.mParameters.value.split("-")[1]); var formt = sap.ui.core.format.DateFormat.getInstance({ pattern: "MM/dd/yyyy" }); this.oMainModel.setProperty("/startDateSetupModel", formt.format(new Date(year, month - 1, 1))); this.oMainModel.setProperty("/minEndDate", new Date(formt.format(new Date(year, month - 1, 1)))); // this.oMainModel.setProperty("/startDateSetupModel", formt.format(new Date(oEvent.mParameters.value))); },
_endDateChange: function (oEvent) { var year = parseInt(oEvent.mParameters.value.split("-")[2]); var month = parseInt(oEvent.mParameters.value.split("-")[1]); var formt = sap.ui.core.format.DateFormat.getInstance({ pattern: "MM/dd/yyyy" }); this.oMainModel.setProperty("/endDateSetupModel", formt.format(new Date(year, month, 0))); this.oMainModel.setProperty("/maxStartDate", new Date(formt.format(new Date(year, month, 0)))); },

The issue is whenever I try to set the start date it automatically sets to the 01 2023 instead of that I want it in a format like the

End Date

End Date is working fine. While debugging I found that if in this line

this.oMainModel.setProperty("/startDateSetupModel", formt.format(new Date(year, month - 1, 1)));

If I change the last parameter of the new Date to any number greater than 12 like 13

this.oMainModel.setProperty("/startDateSetupModel", formt.format(new Date(year, month - 1, 13)));

than it works fine. But I am not able to set it to any number below 12. Can anyone help me with this.

Accepted Solutions (0)

Answers (0)