cancel
Showing results for 
Search instead for 
Did you mean: 

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

Former Member
0 Kudos

Hi,

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')
oToDate.setDateValue(currentDate);
},

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);
oFromDate.setDateValue(oToDate);
},

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.

Thanks,

Tim

Accepted Solutions (1)

Accepted Solutions (1)

former_member340030
Contributor

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({
  oFormatYyyymmdd:null,pattern:"yyyy-MM-dd",
  calendarType:sap.ui.core.CalendarType.Gregorian
  }); // 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");
oFromDate.setDateValue(prevDate);},

manipulateDate:function(date,days,operation)

{ 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;

},

thanks

Viplove

Former Member
0 Kudos

Thanks Viplove!

Answers (0)