aks001

How to split dates

hi,

I want to split dates according to monthsBetween() start date and end date  and I'm confused so if any one has any of the solutions... than plz ... tell me... thanks in advance....  :)

kiranperla

hi u can use the following snippet for ur requirement

Date a = Date.newInstance(2013,10,7);
Date b = Date.newInstance(2014,1,12);
Integer monthDiff = a.monthsBetween(b);
if (b.day() > a.day()) monthDiff++;
System.debug(monthDiff);

aks001

hello.... thanks.. for recognition ... :)
can i do this dynamically ... opportunityLineItem.StartDate__c
...???
Date a = Date.newInstance(opportunityLineItem.StartDate__c );

magicforce9

Hi,

If the opportunityLineItem.StartDate__c field is already in Date field type you don't need to create a new instance. You can calculcate the months by doing this

Integer diff = opportunityLineItem.StartDate__c.monthsBetween(opportunityLineItem.EndDate__c);

But I'm not sure what you exactly mean by splittin the dates ?

aks001

hi,

Yes ....... I want to split dates ... as in , i'm creating records based on the difference of months comes in oli's start date and end date like.. if the difference is 4 then 4 child records will be created ... nd now the problem is i want to create new dates to newly created records ...accordingly... so plz tell me that how to calculate new dates...from original... dates of parent record....

thanks...

magicforce9

Hi...You can try this if meets your requirements.

```//Lets say the start date is 22/6/2013 and end date is 25/10/2013
Integer diff = opportunityLineItem.StartDate__c.monthsBetween(opportunityLineItem.EndDate__c);

// Now the diff of months is 4 and the below variable will slip the number of days into 4 equal parts
Long relativeDays = decimal.ValueOf(opportunityLineItem.StartDate__c.DaysBetween(opportunityLineItem.EndDate__c)/diff).round();

//During creating new child records which you are doing according to number of months add the new date
for(integer i = 0; i<diff; i++)
{
//This should add the each part of realiveryDays to you child object date
child_object__c = new chid_object__c(Child_record_date = opportunityLineItem.StartDate__c.addDays(relativeDays));
}```

aks001
thanks... magicForce...