0
votes

My default date setting in Fiori launchpad is "dd.MM.yyyy, HH:mm". Whenever I get the date from the date picker placed in view, I am getting the date in the above format.

Now I want to send this date to backend through ODataModel which generally accepts date in XML date format (e.g. "2014-12-30"). I tried the below code, but it did not work.

var fromDate = this.byId("fromDate").getValue(); // "30.12.2014, 10:36"
var oDateFormat = DateFormat.getDateTimeInstance({ pattern : "yyyy-MM-dd" }); // DateFormat required from "sap/ui/core/format/DateFormat"
var subFromDate = oDateFormat.format(new Date(fromDate)); // "0NaN-NaN-NaN". 

When I check in debugger mode, the value in subFromDate is "0NaN-NaN-NaN". Please provide your valuable suggestions.

3

3 Answers

1
votes

You can use getDateValue() method instead of getValue.

var fromDate = this.byId("fromDate").getDateValue(); // returns a JS date object
var oDateFormat = DateFormat.getDateTimeInstance({ pattern : "yyyy-MM-dd" }); // DateFormat required from "sap/ui/core/format/DateFormat"
var subFromDate = oDateFormat.format(fromDate); // "2014-12-30"
-1
votes

The fromDate isn't correct. When I using new Date("30.12.2014, 10:36"), the console show message " Invalid Date".

I look for more information about "Date" from MDN(link).

new Date(dateString)

String value representing a date. The string should be in a format recognized by the Date.parse() method. The dateString could be '30 12 2014 10:36'.

So you need to replace the '.' and ',' to ' ' in fromDate first.

-1
votes

You can use display format and value format properties of date time picker.

 new sap.m.DateTimePicker("ED",{
                        valueFormat: "yyyy-MM-ddTHH:mm:ss",
                        displayFormat: "dd-MM-yyyy HH:mm:ss"
                    });

you can easily get the values using

sap.ui.getCore().getControl("ED").getValue();