None of the above answers worked when setting a datetime attribute on a time element. the closest was @Marcos Buarque's answer.
Instead of a formatted date, the rendered output would either print the unix timestamp or a wrongly-formatted date like "2020-09-03ADT00:00:00".
In order to get the proper format for a full date time, I couldn't use Y-m-d\TH:i:s
or Y-m-dTH:i:s
. I needed to split the format into two variables. This is probably some quirk with format_date and twig.
{% set htmlDate = node.getCreatedTime|format_date('custom', 'Y-m-d') %}
{% set htmlTime = node.getCreatedTime|format_date('custom', 'H:i:s') %}
<time datetime="{{ htmlDate }}T{{ htmlTime }}">{{ date }}</time>