Configure default datetime format of Symfony Serializer DateTimeNormalizer

When using the Symfony Serializer Component, the default format for date-times is DateTimeInterface::RFC3339, which produces output like 2005-08-15T15:52:01+00:00.

I needed the extended format with milliseconds or microseconds included. The solution is to overwrite the default DateTimeNormalizer service in your services.yaml:

services:
  serializer.normalizer.datetime:
    class: 'Symfony\Component\Serializer\Normalizer\DateTimeNormalizer'
    arguments:
      -
        !php/const Symfony\Component\Serializer\Normalizer\DateTimeNormalizer::FORMAT_KEY: 'Y-m-d\TH:i:s.uP'
    tags:
      - { name: serializer.normalizer, priority: -910 }

The format string Y-m-d\TH:i:s.uP can be customized to match your specific needs. This configuration applies to all date-times globally across your application, not on a per-property basis.

This was tested with Symfony 5.0.1.