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
2from datetime import datetime
3import pytz
4
5# Create a datetime object (using UTC for the example)
6now = datetime.now(pytz.utc)
7
8# Convert datetime object to an RFC 3339 string
9rfc3339_string = rfc3339.rfc3339(now)
10print(f"Formatted String: {rfc3339_string}")
11
12# Parse an RFC 3339 string back into a datetime object
13# Note: This requires the 'parse' function if supported or standard datetime methods
14# The core utility of tonyg-rfc3339 is the formatting:
15print(f"RFC 3339 Output: {rfc3339.rfc3339(datetime(2011, 1, 1, 12, 34, 56))}")