function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
willingwilling 

I need to add yeare, months and days to a date

I have a situation where I have to add years, months or days based on the values in a variable . Here is what I am doing.
the  variables used have these values

time_cov = 1   
ST[0].SVMXC__Unit_of_Time__c == 'Years' is true 

so it should add 1 year to the date t . But it does not add anything
It does go in the if - statement and  system.debug  statement does get executed.



date t = date.valueOf(WR.SVMXC__Start_Date__c);



if (ST[0].SVMXC__Unit_of_Time__c =='Years' )
    { t.addYears(time_cov) ;
    system.debug(t);
    }

else if (ST[0].SVMXC__Unit_of_Time__c =='Months')
    { t.addMonths(time_cov) ;}

else if (ST[0].SVMXC__Unit_of_Time__c =='Days')
    {     t.addDays(time_cov) ;}

else if (ST[0].SVMXC__Unit_of_Time__c =='Weeks')
    {   t.addDays(time_cov * 7) ;}




Please let me know if I am doing anything wrong
Any help is appriciated
sparkysparky
Hmm, I dunno.
Is time_cov an integer?
are you sure your date t is the value you think it is before the addYears?  Have you tried putting a debug on t before the addYears statement?
willingwilling
I found out the solution. It now works like this.


Date t ;


if (ST[0].SVMXC__Unit_of_Time__c =='Years')
{t = WR.SVMXC__Start_Date__c.addYears(time_cov) ;
// system.debug(t);
}
sparkysparky
Oh, yeah, that makes sense.
I think the problem was your date.ValueOf() in the original.  That doesn't operate on a date value.