Skip to content

Instantly share code, notes, and snippets.

@mattstibbs
Created August 17, 2019 16:00
Show Gist options
  • Star 8 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save mattstibbs/a283f55a124d2de1b1d732daaac1f7f8 to your computer and use it in GitHub Desktop.
Save mattstibbs/a283f55a124d2de1b1d732daaac1f7f8 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