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
  • 2
    DateTime.format() method always return string not DateTime.. Yes also format is method of dateTime not date
    – Ratan Paul
    Commented Mar 9, 2016 at 15:21
  • 1
    .format is method of dateTime not date
    – Himanshu
    Commented Mar 9, 2016 at 15:22

1 Answer 1

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.

You must log in to answer this question.

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