Skip to Content

SAPUI5 Calculate date and set to date picker input field - formating issue

Jan 30, 2017 at 01:27 PM


avatar image
Former Member


I two input fields (date picker) and I want to set default dates to both, dateFrom and dateTo. dateTo contains the current date, I am setting it as default like this:

setCreatedToDate: function() {
var currentDate = new Date();
var oToDate = this.getView().byId('__CreatedOnTo')

This works fine.

For the dateFrom, I need to take current date - 30 days and then trying to set it as default, it returns an error :

Date must be a JavaScript date object; Element sap.m.DatePicker#__xmlview1--__CreatedOnFrom

I have tried to change the format of the date after the calculation part, but it does not work:

setCreatedFromDate: function() {

  var oFormatYyyymmdd = null;
  this.oFormatYyyymmdd = sap.ui.core.format.DateFormat.getInstance({
  oFormatYyyymmdd: null,
  pattern: "yyyy-MM-dd",
  calendarType: sap.ui.core.CalendarType.Gregorian

var fromDate = new Date();
var prevDate = fromDate.setDate(fromDate.getDate() - 30);
var oFromDate = this.getView().byId('__CreatedOnFrom')
var oToDate = this.oFormatYyyymmdd.format(fromDate);

So in what format I need to return the value to the input field that it will work like in above case? Now format yyyy-MM-dd does not work.



10 |10000 characters needed characters left characters exceeded
* Please Login or Register to Answer, Follow or Comment.

1 Answer

Best Answer
VIPLOVE KHUSHALANI Jan 30, 2017 at 02:03 PM

Hi ,

Why are you changing format, its not required just get the 30 days before the current date and set it to oFromDate.

Below code is not required :

 var oFormatYyyymmdd =null;
  this.oFormatYyyymmdd =sap.ui.core.format.DateFormat.getInstance({
  }); // not require

var oToDate = this.oFormatYyyymmdd.format(fromDate); // not require

var prevDate = fromDate.setDate(fromDate.getDate()-30); // incorrect way of getting the 30 days previous days

Corrected code :

var fromDate =newDate();
var oFromDate = this.getView().byId('__CreatedOnFrom')

var prevDate = this._manipulateDate(fromDate,30,"sub");


{ var dateOffset = (24*60*60*1000) * days;

var myDate = new Date();

if(operation === "sub") {

myDate.setTime(date.getTime() - dateOffset);


else if (operation === "add")


myDate.setTime(date.getTime() + dateOffset);


return myDate;




Show 1 Share
10 |10000 characters needed characters left characters exceeded
Former Member

Thanks Viplove!