cancel
Showing results for 
Search instead for 
Did you mean: 

Calculate duration from DateTimeInput

Former Member
0 Kudos

Hi

I am using two sap.m.DateTimeInput controls for Begin and End Date of a trip. Now I want to calculate the duration of that trip based on the selected dates. The duration will be displayed on in a sap.m.Input.

Is there any possibility to start calculation and set the value in the input field after both dates have been selected by the user? Some kind of Listener?

Thank you very much

best regards,

Thomas Maier

Accepted Solutions (1)

Accepted Solutions (1)

Qualiture
Active Contributor
0 Kudos

Hi Thomas,

You could use a formatter function in the input field to calculate the duration:


var oDuration = new sap.m.Input({value:{

    parts : ["/startDate", "/endDate"],

    formatter : function(date1, date2) {

        var days = (date2 - date1)/(1000*60*60*24);

        return days;

    }

}})

where startDate and endDate in this example are the properties bound to both datetimeinput controls

Former Member
0 Kudos

Hi

Thank you for your help so far. Really appreciate that. Now I do the following:

this.oInputDuration.bindValue({

  parts: [

       this.oDateTimeInputBegin.getProperty("value"),

       this.oDateTimeInputEnd.getProperty("value")

         ],

  formatter: function(beginDate, endDate){

     var duration = (endDate - beginDate)/(1000*60*60*24);

     console.log(duration);

     return duration;

  }

  });

oInputDuration in that case is the Input control. oDateTimeInputBegin and oDateTimeInputEnd are the two DateTimeInput controls.

Currently no error is thrown but the value of the Input control stays blank. Any idea?

best regards

Thomas Maier

Qualiture
Active Contributor
0 Kudos

I Think the 'parts' property only takes model property names, not the actual values... Could you bind the datepickers to a model property and use those?

Former Member
0 Kudos

Hi

Yes you were right. Yesterday I changed it to model properties and it was working. Now I have the problem that the calculation of days is returned as NaN but that should be solved rather quickly.

best regards

Thomas Maier

Former Member
0 Kudos

Solved now.

Thank you very much to all of you for your help.

best regards

Thomas Maier

Answers (2)

Answers (2)

Former Member
0 Kudos

Hi Thomas,

You can use data binding to display the calculated value in a text field.

Click on Testpage at the end of the page in the link below:

Documentation/AdvancedTopics/DataBinding/CalcFields – SAPUI5 Wiki (TIP CORE User Interface)

Read the event part on the link below:

You will know more on how an event if fired when a text in a field has changed.

DatePicker - SAPUI5 Demo Kit

Regards,

Ashvin

Former Member
0 Kudos

Hi Thomas,

Use the 'change' event: SAPUI5 SDK - Demo Kit

When one of the two is changed, I'd use javascript to calculate the difference.

Kind regards,

RW