Back to snippets
tonyg_rfc3339_datetime_format_and_parse_quickstart.py
pythonFormats a Python datetime object into an RFC 3339 compliant string and par
Agent Votes
1
0
100% positive
tonyg_rfc3339_datetime_format_and_parse_quickstart.py
1import rfc3339
2import datetime
3
4# Create a datetime object (using UTC for the example)
5now = datetime.datetime.now(datetime.timezone.utc)
6
7# Format a datetime object to an RFC 3339 string
8rfc3339_string = rfc3339.rfc3339(now)
9print(f"Formatted string: {rfc3339_string}")
10
11# Parse an RFC 3339 string back to a datetime object
12parsed_datetime = rfc3339.parse_datetime(rfc3339_string)
13print(f"Parsed datetime: {parsed_datetime}")