Skip to the content.

RFC 5322/2822 Format

Recipes

import email.utils
import time
from datetime import datetime, UTC


# default
>>> email.utils.formatdate()  # current time
'Fri, 12 Aug 2022 13:56:40 GMT'
>>> email.utils.formatdate(usegmt=True)  # current time
'Fri, 12 Aug 2022 13:56:40 GMT'


# timestamp (UTC), for HTTP
>>> timestamp = time.time()
>>> email.utils.formatdate(timestamp, usegmt=True)
'Fri, 12 Aug 2022 13:56:40 GMT'


# datetime
>>> dt = datetime.now()
>>> email.utils.format_datetime(dt)
'Fri, 12 Aug 2022 21:56:40 -0000'


# datetime (UTC), for HTTP
>>> dt = datetime.now(UTC)
>>> email.utils.format_datetime(dt, usegmt=True)
'Fri, 12 Aug 2022 13:56:40 GMT'

References