I have couple of weird problems with the sap.m.DatePicker
. As I always do, I searched for topics via google, they helped me, but not entirely.
What I want to achieve
I would like to have a sap.ui.table.Column
in a sap.ui.table.Table
and this column should have a template: new sap.m.DatePicker()
with date formatted as dd.MM.yyyy
.
What I have done so far
I am using a v2.ODataModel
to populate a sap.ui.table.Table
from an OData service. Everything went well until I decided to have the dates in the corresponding column displayed in a preferred format. At first, I was using only these properties, related to formatting in the DatePicker's constructor:
new DatePicker({
displayFormat: "dd.MM.yyyy",
valueFormat: "yyyy-MM-dd",
value: "{" + fieldName + "}",
//...,
})
In this way, all dates are displayed in the following format
Sun Aug 06 2017 03:00:00 GMT+0300 (FLE Daylight Time)
I remember yesterday reading in a documentation, that when I have binding, displayFormat
will be ignored, which to me is ... I don't understand why is that.
Anyway, I found a solution, which gives the advice to set value like this:
value: {
path: colName,
type: new sap.ui.model.type.Date({
pattern: "dd.MM.yyyy",
//source: { pattern: "yyyy-MM-dd HH:mm:ss:fff" }
})
}
At first, it seems to work well and the date is formatted and displayed correctly, but when I try to modify the date in a row... For example, I choose August 6th 2017. What is saved to the database via the OData service is 2017-06-08 00:00:00.000. This is not August 6th but June 8th. I select August 2nd - in the database, I see 2017-02-08 00:00:00.000 - February 8th. Seems like the month and day get switched. If I select, for example, August 30th, the change is not saved. As far as I understand what the problem is - there is no month with number 30, so it refuses to save it. Totally makes sense. :D
Then I tried to add/uncomment the line source: {}
. Now for every row, the date column shows empty, as if every row in the database has null
as value in that date column.
Next, I tried to remove displayFormat: "dd.MM.yyyy", valueFormat: "yyyy-MM-dd"
from the definition of the DatePicker template - nothing changed. Interesting is - no matter if I remove those two lines or not, I can still change the date of a row (the change gets saved to the database), although month and day are still switched.
Then I came across another solution, which said, that I should add a formatter like this:
value: {
path: colName,
formatter: function(val) {
return sap.ui.core.format.DateFormat.getDateInstance({
pattern: "dd-MM-yyyy"
}).format(val);
}
}
This way, it all seems to work as it should. Now you may ask did I give up using the format dd.MM.yyyy
. Answer is "No!", it just doesn't work.
So, this is the main question, that I want to ask: What's wrong with this formatting? No matter what other symbol I use, comma, dash, it always works (dd-MM-yyyy
or dd,MM,yyyy
). The dates are displayed with the corresponding symbol. When I try to use dd.MM.yyyy
, it errors like that:
and like that:
Thank you all for your help! Will appreciate it!
Edit: I have just come up with the idea to workaround this issue. Unfortunately, it doesn't work and produces the exact same error, described above with pictures. Here is the nonworking solution:
return sap.ui.core.format.DateFormat.getDateInstance({
pattern: "dd-MM-yyyy"
}).format(val).replace(/\-/g, '.');