Difference between revisions of "Using Python datetime with Timezones"
Jump to navigation
Jump to search
PeterHarding (talk | contribs) (Created page with " =Explicit Numerical Offsets from UTC= <pre> from datetime import datetime, timezone dt = datetime.now() # This is a naive datetime value print(dt.isoformat()) print(dt.tzinfo) # Cast this as UTC dt = dt.replace(tzinfo=timezone.utc) # Define the offset offset = "+1000" # Convert as so... tz_aware_dt = dt.astimezone(datetime.strptime(offset, "%z").tzinfo) print(tz_aware_dt.isoformat()) </pre> Category:Python Category:datetime Category:EXamples") |
PeterHarding (talk | contribs) |
||
Line 4: | Line 4: | ||
<pre> | <pre> | ||
#!/usr/bin/env python3 | |||
from datetime import datetime, timezone | from datetime import datetime, timezone | ||
Revision as of 10:19, 5 June 2022
Explicit Numerical Offsets from UTC
#!/usr/bin/env python3 from datetime import datetime, timezone dt = datetime.now() # This is a naive datetime value print(dt.isoformat()) print(dt.tzinfo) # Cast this as UTC dt = dt.replace(tzinfo=timezone.utc) # Define the offset offset = "+1000" # Convert as so... tz_aware_dt = dt.astimezone(datetime.strptime(offset, "%z").tzinfo) print(tz_aware_dt.isoformat())