Create a custom function to convert a field data
In Drupal 7, I can create a custom function in the template.php file


To cut a very long story short, in Drupal 7 you should ideally have been  calling that function in a preprocess function, to prepare variables for the template, rather than calling it from the template directly.

In Drupal 8 it's now Fine™ to call functions directly in the template. But now you can't use PHP directly, so you need to write a Twig extension.

It's very easy, core even has its own that you can copy from. The important thing is to register the service class with a twig.extension tag, so the system picks it up.

See this blog post for a full example.