How to calculate dates between start and end point?

08 Oct '12, 02:58 PM
11,780 Views
No Forum Badges

Hi again,

I´ve one entity in which a start and a end day is stored. Now I want to store a status for each day in another entity. Following an example:

Entity A:

start date: 01.10.2012 end date: 03.10.2012

No I´d like to create for each date from the 1st to the 3rd a row in Entity B.

So the result in Entity B should be:

  1. 01.10.2012
  2. 02.10.2012
  3. 03.10.2012

I hope it´s possible to catch what I mean?

Thank´s!

Felix

 
x 0
Follow
Answer Answer at this question and get points!
Forum Hero - Level 9

Hi,

the code is formally bad written. For example the condition of the while loop must be a compare and not an assignment of a variable (a = b VS a == b).

Here the full code related to your request:

#input Date startDate, Date endDate
#output Date[] resultDates

def startDateMs = startDate.getTime()       
def endDateMs = endDate.getTime()

def numberOfDays = (endDateMs.minus(startDateMs))/86400000
println numberOfDays

def resultDates = [];
resultDates.add(startDate);
int count = 1;
while (count < numberOfDays){
   resultDates.add(startDate + count);
   count ++;
}
resultDates.add(endDate);

return ["resultDates" : resultDates]

   
x 1
No Forum Badges

Hi,

you can find the dates between a start date and an end date by using a Script Unit, which contains the code for calculating the intermediate dates. For example, you could first calculate the number of days between the two dates received as input (e.g. start date = 10/10/12; end date = 10/14/12; number of days = 4). Secondly you could add single days to the start date, until you add all the difference days and you reach the end date (e.g. 10/10/12 + 1 = 10/11/12; 10/10/12 + 2 = 10/12/12 and so on).

To find the difference days between the start and the end date you can use the Groovy Date ".minus" method, that subtracts a date from a another date (both dates are espressed in milliseconds). The difference has to be divided by the number of milliseconds in a day. As result, you get the number of days between two dates. For example:

#input Date startDate, Date endDate

def startDateMs = startDate.getTime()
def endDateMs = endDate.getTime()

def numberOfDays = (endDateMs.minus(startDateMs))/86400000
println numberOfDays

The intermediary dates, calculated by incrementing the start date, could be added to a list which is returned from the Script Unit and passed for example to a Create Unit (working on Entity B) that stores the dates. The Create Unit should have the "Bulk" property enabled, so it can perform multiple object creations at once.

 
x 1
No Forum Badges

Dear Laura,

Thank´s a lot for your reply. I tried to do the loop to add the days in a scipt unit, but it is not working at all (I´ve absolutely no plan about groovy...).

Following my try:

#input Date StartDate, int AddDays
def NewDate = StartDate
def i = 1
while(i=AddDays)
{
  NewDate++;
  i++;
}    
return NewDate

Do you know where the mistakes are?

Thank´s a lot for your help...

Felix

 
x 0
Answer at this question and get points!