Skip to content

Instantly share code, notes, and snippets.

@mikegrima
Last active February 4, 2019 20:15
Show Gist options
  • Save mikegrima/c00b85f8973d976a8c83dd95a0f5100b to your computer and use it in GitHub Desktop.
Save mikegrima/c00b85f8973d976a8c83dd95a0f5100b to your computer and use it in GitHub Desktop.
Strip timezone and microseconds in Python
from datetime import datetime
# Prints a string that looks like: 2017-08-30T21:19:30 (this is a good standard ISO format)
current_time = datetime.utcnow().replace(tzinfo=None, microsecond=0).isoformat()
print(current_time)
# 2019-02-04T19:44:04
# To read in a string like above
date_obj = datetime.strptime(current_time, "%Y-%m-%dT%H:%M:%S")
#####
# Even better than above:
dt_str = datetime.utcnow().replace(tzinfo=None, microsecond=0).isoformat() + 'Z'
print(dt_str)
# 2018-10-23T21:00:24Z
# And to read the above:
date_obj = datetime.strptime(current_time, "%Y-%m-%dT%H:%M:%SZ")
## EPOCHs:
epoch = int(datetime.utcnow().timestamp())
print(epoch)
# 1549340128
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment