SAP Knowledge Base Article - Public

3606441 - Days Calculation Incorrect When Using Standard Delta Function

Symptom

You are using the Delta function in your custom PDI code to calculate the difference between two dates (In days), but you notice that the result are inconsistent/incorrect.

Environment

SAP Cloud for Customer core applications

Reproducing the Issue

Pre-requisite: You have built a custom PDI code to calculate the days using the Delta function.

Example: 

this.StartDate = Date.ParseFromString("20150214");
var OneMonthLater = this.StartDate.AddDuration(Duration.ParseFromString(“P1M”);
this.DeltaAsMonth = this.StartDate.Delta(OneMonthLater);

In the above example the code is using the Delta function as "this.StartDate.Delta(OneMonthLater);". Where OneMonthLater is P1M from the date "20150214". According to this you would expect the days to be returned as 28 days since the date considered here is for the month of February (02). But the returned value is 30 days.

Cause

In this example, the duration fetched is P1M. The system doesn't know which month is being pointed out here and hence the days calculated is 30 days which is the standard for number of days in a month.

The same issue can happen if you are calculating days between 2 dates. When you are using the delta function as our example, in this case the system fetches the duration in P_M_D (where _M and _D are number of months and days respectively). 
So when the gap is more than a month and the duration fetched for example is P3M7D (say 3 Months and 7 days). In this scenario the system doesn't know which are these 3 months and hence the result maybe inconsistent here since of these 3 month one month could be 28 days/30 days/31 days as per the month. 

Resolution

Instead of fetching the duration in month and days, you can directly fetch the duration in days so the calculation will be consistent. You can do this by passing one additional parameter true to the Delta function.

For example:

this.StartDate = Date.ParseFromString("20150214");
var OneMonthLater = this.StartDate.AddDuration(Duration.ParseFromString(“P1M”);
this.DeltaAsDays = this.StartDate.Delta(OneMonthLater,true);

Now as per above example, the days calculated will be 28 days since the duration is fetched directly in days considering the month passed here is February.

Note: As per the Delta function the number of days calculated between the 2 dates will not include the end date and only the start date is considered.
For example if start date is 01-01 and end date is 05-01. Here only the start date (01-01) is considered.

See Also

Date (Reuse Library) | SAP Help Portal

Keywords

Delta , Days , Calculate , Difference , Dates , Inconsistent , Incorrect , Month , PDI , Custom , Code , KBA , AP-RC-BDS-SCR , ByDesign Studio Scripting (ABSL) , How To

Product

SAP Cloud for Customer core applications all versions