0

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.

6
  • date.format() return type is string
    – Ratan Paul
    Jun 10, 2016 at 4:35
  • don't format the date, instead directly assign cands.Start_Date__c = contacts.Start_Date__c;
    – Ratan Paul
    Jun 10, 2016 at 4:38
  • I don't want the time portion of the date, with what you suggested it will give me the time portion and no control over the format of the date. I want to control the format of the state when i do the assignment @Ratan Jun 10, 2016 at 4:40
  • if you just want the date value use 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.
    – Mr.Frodo
    Jun 10, 2016 at 5:01
  • @user1669296 Then I suggest change the cands.Start_Date__c dataType to text since you just want to display formatted date text.
    – Ratan Paul
    Jun 10, 2016 at 5:31

1 Answer 1

1

There is no particular way by which you can make certain dates to appear in different format(Date and DateTime data types). Date formats are decided by locale setting of current user and all dates appear in same format as set by locale setting.

To change locale setting visit user record and change locale. For example for locale English (united states) date format would be mm-dd-yyyy.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .