I tried to find an existing question/answer for my problem, but didn't see anything so I'm trying to format a date in apex as "mm/dd/yyyy", but get the error: "Illegal assignment from String to Date" My code is below.
for( Contact__c contacts : contactrecords ) {
Candidates__c cands = new Candidate__c();
cands.Start_Date__c = contacts.Start_Date__c.format('mm/dd/yyyy');
}
Basically I'm trying to set the start date field equal to the other start date field, but format is as "mm/dd/yyyy'. I understand why the error happens, it seems format()
method returns a string, but don't have a solution to achieve what I want.
date.format()
return type is stringcands.Start_Date__c = contacts.Start_Date__c;
date()
of datetime class.If you don't want time portion you can not save it again in datetime type field because that will always give you time portion.cands.Start_Date__c
dataType to text since you just want to display formatted date text.