0

Trying to convert DAteTime field in local user time zone with following code but get an error:

Compile Error: Method does not exist or incorrect signature: [Date].format(String, String)

TimeZone tz = UserInfo.getTimeZone();
List<Task> allTasks = [Select id, ActivityDate, Description, Subject from Task where Id IN :taskIds];
for (Task t : allTasks)
{
    DateTime dt = t.ActivityDate.format('M/d/yyyy hh:mm a',tz.getId());
}

Please help. Thanks in advance

  • 2
    DateTime.format() method always return string not DateTime.. Yes also format is method of dateTime not date – Ratan Paul Mar 9 '16 at 15:21
  • 1
    .format is method of dateTime not date – Himanshu Mar 9 '16 at 15:22
1

ActivityDate is date on Task object so you need to convert that into datetime first.

enter image description here

TimeZone tz = UserInfo.getTimeZone();
    List<Task> allTasks = [Select id, ActivityDate, Description, Subject from Task where Id IN :taskIds];
    for (Task t : allTasks)
    {
        DateTime activityDate = t.activitydate;
        String dt =  activityDate.format('M/d/yyyy hh:mm a',tz.getId());
        system.debug(dt);
    }

Above code will work for you.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.