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
Frank CarterFrank Carter 

how to set only the month in a date fields

Hello,
I need help. I have a method that receives a list of custom object. In this object there is a date field already valued. I want update only the month of this date with the current month.
I tried with:
Integer m = Date.Today().Month();

 for(Custom_Object__c co: selectedCO) {
                                 
           co.date__c.Month()=m;

Error: Expression cannot be assigned

Can Someone hepl me?


Thanks,
Francesco
Best Answer chosen by Frank Carter
Alain CabonAlain Cabon
Hi,

newInstance(year, month, date) (static): Constructs a Date from Integer representations of the year, month (1=Jan), and day.

 co.date__c = Date.newinstance(  co.date__c.year() , m ,  co.date__c.day() );