How do I format custom field date type in e-mail template?

Hi,

I have a custom field which is “Select datetime” type.
I want to print this field value in my e-mail template that is sent after Correspond action is executed.

When I use this syntax:

{
  $output = "";
  $CFValue = $Ticket->FirstCustomFieldValue('MyCF');
  $output .= "My CFValue:  " . $CFValue;
  $output;
}

it prints the date as “2025-08-21 10:00:00” but this is in some default timezone (possibly UTC) shifted a few hours from my local timezone which is not what I want to be printed as I’d like to have my local timezone printed in template.

For system fields I usually use “>AsString(Format => ‘ISO’)” in template but trying like this:

{
 $output = "";
 $CFValue = $Ticket->FirstCustomFieldValue('MyCF')->AsString(Format => 'ISO');
 $output .= "My CFValue:  " . $CFValue;
 $output;
}

is not working (i.e. emial is not sent out so I guess there is some error in template) for me for my custom field (although it works properly for other system fields like Starts or Due).

How do I format datetime custom field to be printed in local timezone?

Thank you in advance
Szymon

Hello Szymon,

You could create a date object with the value from the CF.
You can then apply AsString to this object to get your local user time. The following worked for us:

my $date = RT::Date->new(RT->SystemUser);
$date->Set(Format => ‘ISO’, Value => $ticket->FirstCustomFieldValue(MyCF));
RT::Logger->info(“User Date: ” . $date->AsString);

Best regards,
Patrick

Hi Patric,

Thank you for suggestion. Unfortunately this line

$date->Set(Format => ‘ISO’, Value => $ticket->FirstCustomFieldValue(MyCF));

doesn’t work for me :frowning: It hrows error when trying to save the template. Something like “Couldn’t compile template codeblock (…) Unrecognized character \x{2018}; marked by ← HERE after Format => ← HERE near column 954 at template line 59”

BTW I’m using RT 5.0.7 if that matters

regards,

Szymon

I think your single quotes around ISO, are the issue. Try replacing them

Copy/paste quotation marks and other symbols from web can give you problems. Delete and insert them manually.

Dear all,

Indeed this was about single quotes! Thank you @G.Booth Starnge it works with single quotes in other place.

So finally based on initial recommendation and my additional try to further format the date I put into my template:

{
 my $CFValue = $Ticket->FirstCustomFieldValue(‘MyCF’);
 $debug_message .= "DEBUG CFValue:                       " . $CFValue. “\n”;
 my $date = RT::Date->new(RT->SystemUser);
 $date->Set(Format => “ISO”, Value => $Ticket->FirstCustomFieldValue(‘MyCF’));
 $debug_message .= "DEBUG Date field AsString:           " . $date->AsString . “\n”;
 $debug_message .= "DEBUG Date field AsString formatted: " . $date->AsString(Format => ‘ISO’,  Seconds => 0) . “\n\n”;
 $debug_message;
}

which resulted in the following output in e-mail sent:

DEBUG CFValue                      : 2025-08-19 12:00:00
DEBUG Date field AsString          : Tue Aug 19 14:00:00 2025
DEBUG Date field AsString formatted: 2025-08-19 14:00

which perfectly solves my problem

Thank you all for your suuport!!

Regards

Szymon

1 Like

As Sollericos pointed out, copy and paste can do fun things with quotes