0

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.

Boghyon Hoffmann
  • 17,103
  • 12
  • 72
  • 170
Raja
  • 21
  • 3
  • 3
  • 9
  • Possible duplicate of [How to Add Date / Time from OData Service Correctly to UI?](https://stackoverflow.com/questions/47593990/how-to-add-date-time-from-odata-service-correctly-to-ui) – Boghyon Hoffmann Jan 27 '18 at 23:25
  • See also [How to convert datetime string to date](https://stackoverflow.com/q/63343915/5846045) – Boghyon Hoffmann Aug 24 '20 at 16:04

3 Answers3

1

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"
Boghyon Hoffmann
  • 17,103
  • 12
  • 72
  • 170
Mahendra Kulkarni
  • 1,437
  • 2
  • 26
  • 35
-1

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.

Eric_wei
  • 1
  • 1
  • Thank you for the help. I formatted date like '30 12 2014 10:36'. Then applied formatting to make it as '2014-12-30'. Unfortunately, it did not work as expected – Raja Dec 30 '14 at 10:26
  • Sorry. The dateString should be 'MM dd yyyy HH:mm', such as '12 30 2014 10:36'. It will work. – Eric_wei Dec 31 '14 at 01:34
-1

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();
Roshni
  • 1