Skip to content

Instantly share code, notes, and snippets.

@thinkjrs
Forked from mattstibbs/put_colon_in_offset.py
Created October 21, 2021 22:01
Show Gist options
  • Save thinkjrs/381968703617ead485b0cf675ba6821d to your computer and use it in GitHub Desktop.
Save thinkjrs/381968703617ead485b0cf675ba6821d to your computer and use it in GitHub Desktop.
Add a colon to timezone offset when using datetime.strftime
import datetime
original_timestamp = datetime.datetime.now()
# Convert datetime object to a string representation
timestamp_string = original_timestamp.strftime("%Y-%m-%dT%H:%M:%S%z")
print(timestamp_string)
# OUTPUT: 2019-08-17T00:00:00+0000
# Add a colon separator to the offset segment
timestamp_string = "{0}:{1}".format(
timestamp_string[:-2],
timestamp_string[-2:]
)
print(timestamp_string)
# OUTPUT: 2019-08-17T00:00:00+00:00
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment