Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
"""
code snippet to add/subtract datetime object
https://github.com/ahmedshahriar
"""
from datetime import datetime, timedelta
# addition
d = datetime.now() + timedelta(days=1, weeks=1, hours=19,seconds=1,milliseconds=1,microseconds=1)
#subtraction
d = datetime.now() - timedelta(days=1, weeks=1, hours=19,seconds=1,milliseconds=1,microseconds=1)
d.strftime('%Y-%m-%d %H:%M:%S')
# output sample
# 2020-12-05 12:05:36
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment